bcm63xx_udc.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465
  1. /*
  2. * bcm63xx_udc.c -- BCM63xx UDC high/full speed USB device controller
  3. *
  4. * Copyright (C) 2012 Kevin Cernekee <cernekee@gmail.com>
  5. * Copyright (C) 2012 Broadcom Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/bitops.h>
  13. #include <linux/bug.h>
  14. #include <linux/clk.h>
  15. #include <linux/compiler.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/delay.h>
  18. #include <linux/device.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/errno.h>
  21. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/ioport.h>
  24. #include <linux/kconfig.h>
  25. #include <linux/kernel.h>
  26. #include <linux/list.h>
  27. #include <linux/module.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/sched.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/slab.h>
  33. #include <linux/timer.h>
  34. #include <linux/usb/ch9.h>
  35. #include <linux/usb/gadget.h>
  36. #include <linux/workqueue.h>
  37. #include <bcm63xx_cpu.h>
  38. #include <bcm63xx_iudma.h>
  39. #include <bcm63xx_dev_usb_usbd.h>
  40. #include <bcm63xx_io.h>
  41. #include <bcm63xx_regs.h>
  42. #define DRV_MODULE_NAME "bcm63xx_udc"
  43. static const char bcm63xx_ep0name[] = "ep0";
  44. static const char *const bcm63xx_ep_name[] = {
  45. bcm63xx_ep0name,
  46. "ep1in-bulk", "ep2out-bulk", "ep3in-int", "ep4out-int",
  47. };
  48. static bool use_fullspeed;
  49. module_param(use_fullspeed, bool, S_IRUGO);
  50. MODULE_PARM_DESC(use_fullspeed, "true for fullspeed only");
  51. /*
  52. * RX IRQ coalescing options:
  53. *
  54. * false (default) - one IRQ per DATAx packet. Slow but reliable. The
  55. * driver is able to pass the "testusb" suite and recover from conditions like:
  56. *
  57. * 1) Device queues up a 2048-byte RX IUDMA transaction on an OUT bulk ep
  58. * 2) Host sends 512 bytes of data
  59. * 3) Host decides to reconfigure the device and sends SET_INTERFACE
  60. * 4) Device shuts down the endpoint and cancels the RX transaction
  61. *
  62. * true - one IRQ per transfer, for transfers <= 2048B. Generates
  63. * considerably fewer IRQs, but error recovery is less robust. Does not
  64. * reliably pass "testusb".
  65. *
  66. * TX always uses coalescing, because we can cancel partially complete TX
  67. * transfers by repeatedly flushing the FIFO. The hardware doesn't allow
  68. * this on RX.
  69. */
  70. static bool irq_coalesce;
  71. module_param(irq_coalesce, bool, S_IRUGO);
  72. MODULE_PARM_DESC(irq_coalesce, "take one IRQ per RX transfer");
  73. #define BCM63XX_NUM_EP 5
  74. #define BCM63XX_NUM_IUDMA 6
  75. #define BCM63XX_NUM_FIFO_PAIRS 3
  76. #define IUDMA_RESET_TIMEOUT_US 10000
  77. #define IUDMA_EP0_RXCHAN 0
  78. #define IUDMA_EP0_TXCHAN 1
  79. #define IUDMA_MAX_FRAGMENT 2048
  80. #define BCM63XX_MAX_CTRL_PKT 64
  81. #define BCMEP_CTRL 0x00
  82. #define BCMEP_ISOC 0x01
  83. #define BCMEP_BULK 0x02
  84. #define BCMEP_INTR 0x03
  85. #define BCMEP_OUT 0x00
  86. #define BCMEP_IN 0x01
  87. #define BCM63XX_SPD_FULL 1
  88. #define BCM63XX_SPD_HIGH 0
  89. #define IUDMA_DMAC_OFFSET 0x200
  90. #define IUDMA_DMAS_OFFSET 0x400
  91. enum bcm63xx_ep0_state {
  92. EP0_REQUEUE,
  93. EP0_IDLE,
  94. EP0_IN_DATA_PHASE_SETUP,
  95. EP0_IN_DATA_PHASE_COMPLETE,
  96. EP0_OUT_DATA_PHASE_SETUP,
  97. EP0_OUT_DATA_PHASE_COMPLETE,
  98. EP0_OUT_STATUS_PHASE,
  99. EP0_IN_FAKE_STATUS_PHASE,
  100. EP0_SHUTDOWN,
  101. };
  102. static const char __maybe_unused bcm63xx_ep0_state_names[][32] = {
  103. "REQUEUE",
  104. "IDLE",
  105. "IN_DATA_PHASE_SETUP",
  106. "IN_DATA_PHASE_COMPLETE",
  107. "OUT_DATA_PHASE_SETUP",
  108. "OUT_DATA_PHASE_COMPLETE",
  109. "OUT_STATUS_PHASE",
  110. "IN_FAKE_STATUS_PHASE",
  111. "SHUTDOWN",
  112. };
  113. /**
  114. * struct iudma_ch_cfg - Static configuration for an IUDMA channel.
  115. * @ep_num: USB endpoint number.
  116. * @n_bds: Number of buffer descriptors in the ring.
  117. * @ep_type: Endpoint type (control, bulk, interrupt).
  118. * @dir: Direction (in, out).
  119. * @n_fifo_slots: Number of FIFO entries to allocate for this channel.
  120. * @max_pkt_hs: Maximum packet size in high speed mode.
  121. * @max_pkt_fs: Maximum packet size in full speed mode.
  122. */
  123. struct iudma_ch_cfg {
  124. int ep_num;
  125. int n_bds;
  126. int ep_type;
  127. int dir;
  128. int n_fifo_slots;
  129. int max_pkt_hs;
  130. int max_pkt_fs;
  131. };
  132. static const struct iudma_ch_cfg iudma_defaults[] = {
  133. /* This controller was designed to support a CDC/RNDIS application.
  134. It may be possible to reconfigure some of the endpoints, but
  135. the hardware limitations (FIFO sizing and number of DMA channels)
  136. may significantly impact flexibility and/or stability. Change
  137. these values at your own risk.
  138. ep_num ep_type n_fifo_slots max_pkt_fs
  139. idx | n_bds | dir | max_pkt_hs |
  140. | | | | | | | | */
  141. [0] = { -1, 4, BCMEP_CTRL, BCMEP_OUT, 32, 64, 64 },
  142. [1] = { 0, 4, BCMEP_CTRL, BCMEP_OUT, 32, 64, 64 },
  143. [2] = { 2, 16, BCMEP_BULK, BCMEP_OUT, 128, 512, 64 },
  144. [3] = { 1, 16, BCMEP_BULK, BCMEP_IN, 128, 512, 64 },
  145. [4] = { 4, 4, BCMEP_INTR, BCMEP_OUT, 32, 64, 64 },
  146. [5] = { 3, 4, BCMEP_INTR, BCMEP_IN, 32, 64, 64 },
  147. };
  148. struct bcm63xx_udc;
  149. /**
  150. * struct iudma_ch - Represents the current state of a single IUDMA channel.
  151. * @ch_idx: IUDMA channel index (0 to BCM63XX_NUM_IUDMA-1).
  152. * @ep_num: USB endpoint number. -1 for ep0 RX.
  153. * @enabled: Whether bcm63xx_ep_enable() has been called.
  154. * @max_pkt: "Chunk size" on the USB interface. Based on interface speed.
  155. * @is_tx: true for TX, false for RX.
  156. * @bep: Pointer to the associated endpoint. NULL for ep0 RX.
  157. * @udc: Reference to the device controller.
  158. * @read_bd: Next buffer descriptor to reap from the hardware.
  159. * @write_bd: Next BD available for a new packet.
  160. * @end_bd: Points to the final BD in the ring.
  161. * @n_bds_used: Number of BD entries currently occupied.
  162. * @bd_ring: Base pointer to the BD ring.
  163. * @bd_ring_dma: Physical (DMA) address of bd_ring.
  164. * @n_bds: Total number of BDs in the ring.
  165. *
  166. * ep0 has two IUDMA channels (IUDMA_EP0_RXCHAN and IUDMA_EP0_TXCHAN), as it is
  167. * bidirectional. The "struct usb_ep" associated with ep0 is for TX (IN)
  168. * only.
  169. *
  170. * Each bulk/intr endpoint has a single IUDMA channel and a single
  171. * struct usb_ep.
  172. */
  173. struct iudma_ch {
  174. unsigned int ch_idx;
  175. int ep_num;
  176. bool enabled;
  177. int max_pkt;
  178. bool is_tx;
  179. struct bcm63xx_ep *bep;
  180. struct bcm63xx_udc *udc;
  181. struct bcm_enet_desc *read_bd;
  182. struct bcm_enet_desc *write_bd;
  183. struct bcm_enet_desc *end_bd;
  184. int n_bds_used;
  185. struct bcm_enet_desc *bd_ring;
  186. dma_addr_t bd_ring_dma;
  187. unsigned int n_bds;
  188. };
  189. /**
  190. * struct bcm63xx_ep - Internal (driver) state of a single endpoint.
  191. * @ep_num: USB endpoint number.
  192. * @iudma: Pointer to IUDMA channel state.
  193. * @ep: USB gadget layer representation of the EP.
  194. * @udc: Reference to the device controller.
  195. * @queue: Linked list of outstanding requests for this EP.
  196. * @halted: 1 if the EP is stalled; 0 otherwise.
  197. */
  198. struct bcm63xx_ep {
  199. unsigned int ep_num;
  200. struct iudma_ch *iudma;
  201. struct usb_ep ep;
  202. struct bcm63xx_udc *udc;
  203. struct list_head queue;
  204. unsigned halted:1;
  205. };
  206. /**
  207. * struct bcm63xx_req - Internal (driver) state of a single request.
  208. * @queue: Links back to the EP's request list.
  209. * @req: USB gadget layer representation of the request.
  210. * @offset: Current byte offset into the data buffer (next byte to queue).
  211. * @bd_bytes: Number of data bytes in outstanding BD entries.
  212. * @iudma: IUDMA channel used for the request.
  213. */
  214. struct bcm63xx_req {
  215. struct list_head queue; /* ep's requests */
  216. struct usb_request req;
  217. unsigned int offset;
  218. unsigned int bd_bytes;
  219. struct iudma_ch *iudma;
  220. };
  221. /**
  222. * struct bcm63xx_udc - Driver/hardware private context.
  223. * @lock: Spinlock to mediate access to this struct, and (most) HW regs.
  224. * @dev: Generic Linux device structure.
  225. * @pd: Platform data (board/port info).
  226. * @usbd_clk: Clock descriptor for the USB device block.
  227. * @usbh_clk: Clock descriptor for the USB host block.
  228. * @gadget: USB slave device.
  229. * @driver: Driver for USB slave devices.
  230. * @usbd_regs: Base address of the USBD/USB20D block.
  231. * @iudma_regs: Base address of the USBD's associated IUDMA block.
  232. * @bep: Array of endpoints, including ep0.
  233. * @iudma: Array of all IUDMA channels used by this controller.
  234. * @cfg: USB configuration number, from SET_CONFIGURATION wValue.
  235. * @iface: USB interface number, from SET_INTERFACE wIndex.
  236. * @alt_iface: USB alt interface number, from SET_INTERFACE wValue.
  237. * @ep0_ctrl_req: Request object for bcm63xx_udc-initiated ep0 transactions.
  238. * @ep0_ctrl_buf: Data buffer for ep0_ctrl_req.
  239. * @ep0state: Current state of the ep0 state machine.
  240. * @ep0_wq: Workqueue struct used to wake up the ep0 state machine.
  241. * @wedgemap: Bitmap of wedged endpoints.
  242. * @ep0_req_reset: USB reset is pending.
  243. * @ep0_req_set_cfg: Need to spoof a SET_CONFIGURATION packet.
  244. * @ep0_req_set_iface: Need to spoof a SET_INTERFACE packet.
  245. * @ep0_req_shutdown: Driver is shutting down; requesting ep0 to halt activity.
  246. * @ep0_req_completed: ep0 request has completed; worker has not seen it yet.
  247. * @ep0_reply: Pending reply from gadget driver.
  248. * @ep0_request: Outstanding ep0 request.
  249. * @debugfs_root: debugfs directory: /sys/kernel/debug/<DRV_MODULE_NAME>.
  250. * @debugfs_usbd: debugfs file "usbd" for controller state.
  251. * @debugfs_iudma: debugfs file "usbd" for IUDMA state.
  252. */
  253. struct bcm63xx_udc {
  254. spinlock_t lock;
  255. struct device *dev;
  256. struct bcm63xx_usbd_platform_data *pd;
  257. struct clk *usbd_clk;
  258. struct clk *usbh_clk;
  259. struct usb_gadget gadget;
  260. struct usb_gadget_driver *driver;
  261. void __iomem *usbd_regs;
  262. void __iomem *iudma_regs;
  263. struct bcm63xx_ep bep[BCM63XX_NUM_EP];
  264. struct iudma_ch iudma[BCM63XX_NUM_IUDMA];
  265. int cfg;
  266. int iface;
  267. int alt_iface;
  268. struct bcm63xx_req ep0_ctrl_req;
  269. u8 *ep0_ctrl_buf;
  270. int ep0state;
  271. struct work_struct ep0_wq;
  272. unsigned long wedgemap;
  273. unsigned ep0_req_reset:1;
  274. unsigned ep0_req_set_cfg:1;
  275. unsigned ep0_req_set_iface:1;
  276. unsigned ep0_req_shutdown:1;
  277. unsigned ep0_req_completed:1;
  278. struct usb_request *ep0_reply;
  279. struct usb_request *ep0_request;
  280. struct dentry *debugfs_root;
  281. struct dentry *debugfs_usbd;
  282. struct dentry *debugfs_iudma;
  283. };
  284. static const struct usb_ep_ops bcm63xx_udc_ep_ops;
  285. /***********************************************************************
  286. * Convenience functions
  287. ***********************************************************************/
  288. static inline struct bcm63xx_udc *gadget_to_udc(struct usb_gadget *g)
  289. {
  290. return container_of(g, struct bcm63xx_udc, gadget);
  291. }
  292. static inline struct bcm63xx_ep *our_ep(struct usb_ep *ep)
  293. {
  294. return container_of(ep, struct bcm63xx_ep, ep);
  295. }
  296. static inline struct bcm63xx_req *our_req(struct usb_request *req)
  297. {
  298. return container_of(req, struct bcm63xx_req, req);
  299. }
  300. static inline u32 usbd_readl(struct bcm63xx_udc *udc, u32 off)
  301. {
  302. return bcm_readl(udc->usbd_regs + off);
  303. }
  304. static inline void usbd_writel(struct bcm63xx_udc *udc, u32 val, u32 off)
  305. {
  306. bcm_writel(val, udc->usbd_regs + off);
  307. }
  308. static inline u32 usb_dma_readl(struct bcm63xx_udc *udc, u32 off)
  309. {
  310. return bcm_readl(udc->iudma_regs + off);
  311. }
  312. static inline void usb_dma_writel(struct bcm63xx_udc *udc, u32 val, u32 off)
  313. {
  314. bcm_writel(val, udc->iudma_regs + off);
  315. }
  316. static inline u32 usb_dmac_readl(struct bcm63xx_udc *udc, u32 off)
  317. {
  318. return bcm_readl(udc->iudma_regs + IUDMA_DMAC_OFFSET + off);
  319. }
  320. static inline void usb_dmac_writel(struct bcm63xx_udc *udc, u32 val, u32 off)
  321. {
  322. bcm_writel(val, udc->iudma_regs + IUDMA_DMAC_OFFSET + off);
  323. }
  324. static inline u32 usb_dmas_readl(struct bcm63xx_udc *udc, u32 off)
  325. {
  326. return bcm_readl(udc->iudma_regs + IUDMA_DMAS_OFFSET + off);
  327. }
  328. static inline void usb_dmas_writel(struct bcm63xx_udc *udc, u32 val, u32 off)
  329. {
  330. bcm_writel(val, udc->iudma_regs + IUDMA_DMAS_OFFSET + off);
  331. }
  332. static inline void set_clocks(struct bcm63xx_udc *udc, bool is_enabled)
  333. {
  334. if (is_enabled) {
  335. clk_enable(udc->usbh_clk);
  336. clk_enable(udc->usbd_clk);
  337. udelay(10);
  338. } else {
  339. clk_disable(udc->usbd_clk);
  340. clk_disable(udc->usbh_clk);
  341. }
  342. }
  343. /***********************************************************************
  344. * Low-level IUDMA / FIFO operations
  345. ***********************************************************************/
  346. /**
  347. * bcm63xx_ep_dma_select - Helper function to set up the init_sel signal.
  348. * @udc: Reference to the device controller.
  349. * @idx: Desired init_sel value.
  350. *
  351. * The "init_sel" signal is used as a selection index for both endpoints
  352. * and IUDMA channels. Since these do not map 1:1, the use of this signal
  353. * depends on the context.
  354. */
  355. static void bcm63xx_ep_dma_select(struct bcm63xx_udc *udc, int idx)
  356. {
  357. u32 val = usbd_readl(udc, USBD_CONTROL_REG);
  358. val &= ~USBD_CONTROL_INIT_SEL_MASK;
  359. val |= idx << USBD_CONTROL_INIT_SEL_SHIFT;
  360. usbd_writel(udc, val, USBD_CONTROL_REG);
  361. }
  362. /**
  363. * bcm63xx_set_stall - Enable/disable stall on one endpoint.
  364. * @udc: Reference to the device controller.
  365. * @bep: Endpoint on which to operate.
  366. * @is_stalled: true to enable stall, false to disable.
  367. *
  368. * See notes in bcm63xx_update_wedge() regarding automatic clearing of
  369. * halt/stall conditions.
  370. */
  371. static void bcm63xx_set_stall(struct bcm63xx_udc *udc, struct bcm63xx_ep *bep,
  372. bool is_stalled)
  373. {
  374. u32 val;
  375. val = USBD_STALL_UPDATE_MASK |
  376. (is_stalled ? USBD_STALL_ENABLE_MASK : 0) |
  377. (bep->ep_num << USBD_STALL_EPNUM_SHIFT);
  378. usbd_writel(udc, val, USBD_STALL_REG);
  379. }
  380. /**
  381. * bcm63xx_fifo_setup - (Re)initialize FIFO boundaries and settings.
  382. * @udc: Reference to the device controller.
  383. *
  384. * These parameters depend on the USB link speed. Settings are
  385. * per-IUDMA-channel-pair.
  386. */
  387. static void bcm63xx_fifo_setup(struct bcm63xx_udc *udc)
  388. {
  389. int is_hs = udc->gadget.speed == USB_SPEED_HIGH;
  390. u32 i, val, rx_fifo_slot, tx_fifo_slot;
  391. /* set up FIFO boundaries and packet sizes; this is done in pairs */
  392. rx_fifo_slot = tx_fifo_slot = 0;
  393. for (i = 0; i < BCM63XX_NUM_IUDMA; i += 2) {
  394. const struct iudma_ch_cfg *rx_cfg = &iudma_defaults[i];
  395. const struct iudma_ch_cfg *tx_cfg = &iudma_defaults[i + 1];
  396. bcm63xx_ep_dma_select(udc, i >> 1);
  397. val = (rx_fifo_slot << USBD_RXFIFO_CONFIG_START_SHIFT) |
  398. ((rx_fifo_slot + rx_cfg->n_fifo_slots - 1) <<
  399. USBD_RXFIFO_CONFIG_END_SHIFT);
  400. rx_fifo_slot += rx_cfg->n_fifo_slots;
  401. usbd_writel(udc, val, USBD_RXFIFO_CONFIG_REG);
  402. usbd_writel(udc,
  403. is_hs ? rx_cfg->max_pkt_hs : rx_cfg->max_pkt_fs,
  404. USBD_RXFIFO_EPSIZE_REG);
  405. val = (tx_fifo_slot << USBD_TXFIFO_CONFIG_START_SHIFT) |
  406. ((tx_fifo_slot + tx_cfg->n_fifo_slots - 1) <<
  407. USBD_TXFIFO_CONFIG_END_SHIFT);
  408. tx_fifo_slot += tx_cfg->n_fifo_slots;
  409. usbd_writel(udc, val, USBD_TXFIFO_CONFIG_REG);
  410. usbd_writel(udc,
  411. is_hs ? tx_cfg->max_pkt_hs : tx_cfg->max_pkt_fs,
  412. USBD_TXFIFO_EPSIZE_REG);
  413. usbd_readl(udc, USBD_TXFIFO_EPSIZE_REG);
  414. }
  415. }
  416. /**
  417. * bcm63xx_fifo_reset_ep - Flush a single endpoint's FIFO.
  418. * @udc: Reference to the device controller.
  419. * @ep_num: Endpoint number.
  420. */
  421. static void bcm63xx_fifo_reset_ep(struct bcm63xx_udc *udc, int ep_num)
  422. {
  423. u32 val;
  424. bcm63xx_ep_dma_select(udc, ep_num);
  425. val = usbd_readl(udc, USBD_CONTROL_REG);
  426. val |= USBD_CONTROL_FIFO_RESET_MASK;
  427. usbd_writel(udc, val, USBD_CONTROL_REG);
  428. usbd_readl(udc, USBD_CONTROL_REG);
  429. }
  430. /**
  431. * bcm63xx_fifo_reset - Flush all hardware FIFOs.
  432. * @udc: Reference to the device controller.
  433. */
  434. static void bcm63xx_fifo_reset(struct bcm63xx_udc *udc)
  435. {
  436. int i;
  437. for (i = 0; i < BCM63XX_NUM_FIFO_PAIRS; i++)
  438. bcm63xx_fifo_reset_ep(udc, i);
  439. }
  440. /**
  441. * bcm63xx_ep_init - Initial (one-time) endpoint initialization.
  442. * @udc: Reference to the device controller.
  443. */
  444. static void bcm63xx_ep_init(struct bcm63xx_udc *udc)
  445. {
  446. u32 i, val;
  447. for (i = 0; i < BCM63XX_NUM_IUDMA; i++) {
  448. const struct iudma_ch_cfg *cfg = &iudma_defaults[i];
  449. if (cfg->ep_num < 0)
  450. continue;
  451. bcm63xx_ep_dma_select(udc, cfg->ep_num);
  452. val = (cfg->ep_type << USBD_EPNUM_TYPEMAP_TYPE_SHIFT) |
  453. ((i >> 1) << USBD_EPNUM_TYPEMAP_DMA_CH_SHIFT);
  454. usbd_writel(udc, val, USBD_EPNUM_TYPEMAP_REG);
  455. }
  456. }
  457. /**
  458. * bcm63xx_ep_setup - Configure per-endpoint settings.
  459. * @udc: Reference to the device controller.
  460. *
  461. * This needs to be rerun if the speed/cfg/intf/altintf changes.
  462. */
  463. static void bcm63xx_ep_setup(struct bcm63xx_udc *udc)
  464. {
  465. u32 val, i;
  466. usbd_writel(udc, USBD_CSR_SETUPADDR_DEF, USBD_CSR_SETUPADDR_REG);
  467. for (i = 0; i < BCM63XX_NUM_IUDMA; i++) {
  468. const struct iudma_ch_cfg *cfg = &iudma_defaults[i];
  469. int max_pkt = udc->gadget.speed == USB_SPEED_HIGH ?
  470. cfg->max_pkt_hs : cfg->max_pkt_fs;
  471. int idx = cfg->ep_num;
  472. udc->iudma[i].max_pkt = max_pkt;
  473. if (idx < 0)
  474. continue;
  475. udc->bep[idx].ep.maxpacket = max_pkt;
  476. val = (idx << USBD_CSR_EP_LOG_SHIFT) |
  477. (cfg->dir << USBD_CSR_EP_DIR_SHIFT) |
  478. (cfg->ep_type << USBD_CSR_EP_TYPE_SHIFT) |
  479. (udc->cfg << USBD_CSR_EP_CFG_SHIFT) |
  480. (udc->iface << USBD_CSR_EP_IFACE_SHIFT) |
  481. (udc->alt_iface << USBD_CSR_EP_ALTIFACE_SHIFT) |
  482. (max_pkt << USBD_CSR_EP_MAXPKT_SHIFT);
  483. usbd_writel(udc, val, USBD_CSR_EP_REG(idx));
  484. }
  485. }
  486. /**
  487. * iudma_write - Queue a single IUDMA transaction.
  488. * @udc: Reference to the device controller.
  489. * @iudma: IUDMA channel to use.
  490. * @breq: Request containing the transaction data.
  491. *
  492. * For RX IUDMA, this will queue a single buffer descriptor, as RX IUDMA
  493. * does not honor SOP/EOP so the handling of multiple buffers is ambiguous.
  494. * So iudma_write() may be called several times to fulfill a single
  495. * usb_request.
  496. *
  497. * For TX IUDMA, this can queue multiple buffer descriptors if needed.
  498. */
  499. static void iudma_write(struct bcm63xx_udc *udc, struct iudma_ch *iudma,
  500. struct bcm63xx_req *breq)
  501. {
  502. int first_bd = 1, last_bd = 0, extra_zero_pkt = 0;
  503. unsigned int bytes_left = breq->req.length - breq->offset;
  504. const int max_bd_bytes = !irq_coalesce && !iudma->is_tx ?
  505. iudma->max_pkt : IUDMA_MAX_FRAGMENT;
  506. iudma->n_bds_used = 0;
  507. breq->bd_bytes = 0;
  508. breq->iudma = iudma;
  509. if ((bytes_left % iudma->max_pkt == 0) && bytes_left && breq->req.zero)
  510. extra_zero_pkt = 1;
  511. do {
  512. struct bcm_enet_desc *d = iudma->write_bd;
  513. u32 dmaflags = 0;
  514. unsigned int n_bytes;
  515. if (d == iudma->end_bd) {
  516. dmaflags |= DMADESC_WRAP_MASK;
  517. iudma->write_bd = iudma->bd_ring;
  518. } else {
  519. iudma->write_bd++;
  520. }
  521. iudma->n_bds_used++;
  522. n_bytes = min_t(int, bytes_left, max_bd_bytes);
  523. if (n_bytes)
  524. dmaflags |= n_bytes << DMADESC_LENGTH_SHIFT;
  525. else
  526. dmaflags |= (1 << DMADESC_LENGTH_SHIFT) |
  527. DMADESC_USB_ZERO_MASK;
  528. dmaflags |= DMADESC_OWNER_MASK;
  529. if (first_bd) {
  530. dmaflags |= DMADESC_SOP_MASK;
  531. first_bd = 0;
  532. }
  533. /*
  534. * extra_zero_pkt forces one more iteration through the loop
  535. * after all data is queued up, to send the zero packet
  536. */
  537. if (extra_zero_pkt && !bytes_left)
  538. extra_zero_pkt = 0;
  539. if (!iudma->is_tx || iudma->n_bds_used == iudma->n_bds ||
  540. (n_bytes == bytes_left && !extra_zero_pkt)) {
  541. last_bd = 1;
  542. dmaflags |= DMADESC_EOP_MASK;
  543. }
  544. d->address = breq->req.dma + breq->offset;
  545. mb();
  546. d->len_stat = dmaflags;
  547. breq->offset += n_bytes;
  548. breq->bd_bytes += n_bytes;
  549. bytes_left -= n_bytes;
  550. } while (!last_bd);
  551. usb_dmac_writel(udc, ENETDMAC_CHANCFG_EN_MASK,
  552. ENETDMAC_CHANCFG_REG(iudma->ch_idx));
  553. }
  554. /**
  555. * iudma_read - Check for IUDMA buffer completion.
  556. * @udc: Reference to the device controller.
  557. * @iudma: IUDMA channel to use.
  558. *
  559. * This checks to see if ALL of the outstanding BDs on the DMA channel
  560. * have been filled. If so, it returns the actual transfer length;
  561. * otherwise it returns -EBUSY.
  562. */
  563. static int iudma_read(struct bcm63xx_udc *udc, struct iudma_ch *iudma)
  564. {
  565. int i, actual_len = 0;
  566. struct bcm_enet_desc *d = iudma->read_bd;
  567. if (!iudma->n_bds_used)
  568. return -EINVAL;
  569. for (i = 0; i < iudma->n_bds_used; i++) {
  570. u32 dmaflags;
  571. dmaflags = d->len_stat;
  572. if (dmaflags & DMADESC_OWNER_MASK)
  573. return -EBUSY;
  574. actual_len += (dmaflags & DMADESC_LENGTH_MASK) >>
  575. DMADESC_LENGTH_SHIFT;
  576. if (d == iudma->end_bd)
  577. d = iudma->bd_ring;
  578. else
  579. d++;
  580. }
  581. iudma->read_bd = d;
  582. iudma->n_bds_used = 0;
  583. return actual_len;
  584. }
  585. /**
  586. * iudma_reset_channel - Stop DMA on a single channel.
  587. * @udc: Reference to the device controller.
  588. * @iudma: IUDMA channel to reset.
  589. */
  590. static void iudma_reset_channel(struct bcm63xx_udc *udc, struct iudma_ch *iudma)
  591. {
  592. int timeout = IUDMA_RESET_TIMEOUT_US;
  593. struct bcm_enet_desc *d;
  594. int ch_idx = iudma->ch_idx;
  595. if (!iudma->is_tx)
  596. bcm63xx_fifo_reset_ep(udc, max(0, iudma->ep_num));
  597. /* stop DMA, then wait for the hardware to wrap up */
  598. usb_dmac_writel(udc, 0, ENETDMAC_CHANCFG_REG(ch_idx));
  599. while (usb_dmac_readl(udc, ENETDMAC_CHANCFG_REG(ch_idx)) &
  600. ENETDMAC_CHANCFG_EN_MASK) {
  601. udelay(1);
  602. /* repeatedly flush the FIFO data until the BD completes */
  603. if (iudma->is_tx && iudma->ep_num >= 0)
  604. bcm63xx_fifo_reset_ep(udc, iudma->ep_num);
  605. if (!timeout--) {
  606. dev_err(udc->dev, "can't reset IUDMA channel %d\n",
  607. ch_idx);
  608. break;
  609. }
  610. if (timeout == IUDMA_RESET_TIMEOUT_US / 2) {
  611. dev_warn(udc->dev, "forcibly halting IUDMA channel %d\n",
  612. ch_idx);
  613. usb_dmac_writel(udc, ENETDMAC_CHANCFG_BUFHALT_MASK,
  614. ENETDMAC_CHANCFG_REG(ch_idx));
  615. }
  616. }
  617. usb_dmac_writel(udc, ~0, ENETDMAC_IR_REG(ch_idx));
  618. /* don't leave "live" HW-owned entries for the next guy to step on */
  619. for (d = iudma->bd_ring; d <= iudma->end_bd; d++)
  620. d->len_stat = 0;
  621. mb();
  622. iudma->read_bd = iudma->write_bd = iudma->bd_ring;
  623. iudma->n_bds_used = 0;
  624. /* set up IRQs, UBUS burst size, and BD base for this channel */
  625. usb_dmac_writel(udc, ENETDMAC_IR_BUFDONE_MASK,
  626. ENETDMAC_IRMASK_REG(ch_idx));
  627. usb_dmac_writel(udc, 8, ENETDMAC_MAXBURST_REG(ch_idx));
  628. usb_dmas_writel(udc, iudma->bd_ring_dma, ENETDMAS_RSTART_REG(ch_idx));
  629. usb_dmas_writel(udc, 0, ENETDMAS_SRAM2_REG(ch_idx));
  630. }
  631. /**
  632. * iudma_init_channel - One-time IUDMA channel initialization.
  633. * @udc: Reference to the device controller.
  634. * @ch_idx: Channel to initialize.
  635. */
  636. static int iudma_init_channel(struct bcm63xx_udc *udc, unsigned int ch_idx)
  637. {
  638. struct iudma_ch *iudma = &udc->iudma[ch_idx];
  639. const struct iudma_ch_cfg *cfg = &iudma_defaults[ch_idx];
  640. unsigned int n_bds = cfg->n_bds;
  641. struct bcm63xx_ep *bep = NULL;
  642. iudma->ep_num = cfg->ep_num;
  643. iudma->ch_idx = ch_idx;
  644. iudma->is_tx = !!(ch_idx & 0x01);
  645. if (iudma->ep_num >= 0) {
  646. bep = &udc->bep[iudma->ep_num];
  647. bep->iudma = iudma;
  648. INIT_LIST_HEAD(&bep->queue);
  649. }
  650. iudma->bep = bep;
  651. iudma->udc = udc;
  652. /* ep0 is always active; others are controlled by the gadget driver */
  653. if (iudma->ep_num <= 0)
  654. iudma->enabled = true;
  655. iudma->n_bds = n_bds;
  656. iudma->bd_ring = dmam_alloc_coherent(udc->dev,
  657. n_bds * sizeof(struct bcm_enet_desc),
  658. &iudma->bd_ring_dma, GFP_KERNEL);
  659. if (!iudma->bd_ring)
  660. return -ENOMEM;
  661. iudma->end_bd = &iudma->bd_ring[n_bds - 1];
  662. return 0;
  663. }
  664. /**
  665. * iudma_init - One-time initialization of all IUDMA channels.
  666. * @udc: Reference to the device controller.
  667. *
  668. * Enable DMA, flush channels, and enable global IUDMA IRQs.
  669. */
  670. static int iudma_init(struct bcm63xx_udc *udc)
  671. {
  672. int i, rc;
  673. usb_dma_writel(udc, ENETDMA_CFG_EN_MASK, ENETDMA_CFG_REG);
  674. for (i = 0; i < BCM63XX_NUM_IUDMA; i++) {
  675. rc = iudma_init_channel(udc, i);
  676. if (rc)
  677. return rc;
  678. iudma_reset_channel(udc, &udc->iudma[i]);
  679. }
  680. usb_dma_writel(udc, BIT(BCM63XX_NUM_IUDMA)-1, ENETDMA_GLB_IRQMASK_REG);
  681. return 0;
  682. }
  683. /**
  684. * iudma_uninit - Uninitialize IUDMA channels.
  685. * @udc: Reference to the device controller.
  686. *
  687. * Kill global IUDMA IRQs, flush channels, and kill DMA.
  688. */
  689. static void iudma_uninit(struct bcm63xx_udc *udc)
  690. {
  691. int i;
  692. usb_dma_writel(udc, 0, ENETDMA_GLB_IRQMASK_REG);
  693. for (i = 0; i < BCM63XX_NUM_IUDMA; i++)
  694. iudma_reset_channel(udc, &udc->iudma[i]);
  695. usb_dma_writel(udc, 0, ENETDMA_CFG_REG);
  696. }
  697. /***********************************************************************
  698. * Other low-level USBD operations
  699. ***********************************************************************/
  700. /**
  701. * bcm63xx_set_ctrl_irqs - Mask/unmask control path interrupts.
  702. * @udc: Reference to the device controller.
  703. * @enable_irqs: true to enable, false to disable.
  704. */
  705. static void bcm63xx_set_ctrl_irqs(struct bcm63xx_udc *udc, bool enable_irqs)
  706. {
  707. u32 val;
  708. usbd_writel(udc, 0, USBD_STATUS_REG);
  709. val = BIT(USBD_EVENT_IRQ_USB_RESET) |
  710. BIT(USBD_EVENT_IRQ_SETUP) |
  711. BIT(USBD_EVENT_IRQ_SETCFG) |
  712. BIT(USBD_EVENT_IRQ_SETINTF) |
  713. BIT(USBD_EVENT_IRQ_USB_LINK);
  714. usbd_writel(udc, enable_irqs ? val : 0, USBD_EVENT_IRQ_MASK_REG);
  715. usbd_writel(udc, val, USBD_EVENT_IRQ_STATUS_REG);
  716. }
  717. /**
  718. * bcm63xx_select_phy_mode - Select between USB device and host mode.
  719. * @udc: Reference to the device controller.
  720. * @is_device: true for device, false for host.
  721. *
  722. * This should probably be reworked to use the drivers/usb/otg
  723. * infrastructure.
  724. *
  725. * By default, the AFE/pullups are disabled in device mode, until
  726. * bcm63xx_select_pullup() is called.
  727. */
  728. static void bcm63xx_select_phy_mode(struct bcm63xx_udc *udc, bool is_device)
  729. {
  730. u32 val, portmask = BIT(udc->pd->port_no);
  731. if (BCMCPU_IS_6328()) {
  732. /* configure pinmux to sense VBUS signal */
  733. val = bcm_gpio_readl(GPIO_PINMUX_OTHR_REG);
  734. val &= ~GPIO_PINMUX_OTHR_6328_USB_MASK;
  735. val |= is_device ? GPIO_PINMUX_OTHR_6328_USB_DEV :
  736. GPIO_PINMUX_OTHR_6328_USB_HOST;
  737. bcm_gpio_writel(val, GPIO_PINMUX_OTHR_REG);
  738. }
  739. val = bcm_rset_readl(RSET_USBH_PRIV, USBH_PRIV_UTMI_CTL_6368_REG);
  740. if (is_device) {
  741. val |= (portmask << USBH_PRIV_UTMI_CTL_HOSTB_SHIFT);
  742. val |= (portmask << USBH_PRIV_UTMI_CTL_NODRIV_SHIFT);
  743. } else {
  744. val &= ~(portmask << USBH_PRIV_UTMI_CTL_HOSTB_SHIFT);
  745. val &= ~(portmask << USBH_PRIV_UTMI_CTL_NODRIV_SHIFT);
  746. }
  747. bcm_rset_writel(RSET_USBH_PRIV, val, USBH_PRIV_UTMI_CTL_6368_REG);
  748. val = bcm_rset_readl(RSET_USBH_PRIV, USBH_PRIV_SWAP_6368_REG);
  749. if (is_device)
  750. val |= USBH_PRIV_SWAP_USBD_MASK;
  751. else
  752. val &= ~USBH_PRIV_SWAP_USBD_MASK;
  753. bcm_rset_writel(RSET_USBH_PRIV, val, USBH_PRIV_SWAP_6368_REG);
  754. }
  755. /**
  756. * bcm63xx_select_pullup - Enable/disable the pullup on D+
  757. * @udc: Reference to the device controller.
  758. * @is_on: true to enable the pullup, false to disable.
  759. *
  760. * If the pullup is active, the host will sense a FS/HS device connected to
  761. * the port. If the pullup is inactive, the host will think the USB
  762. * device has been disconnected.
  763. */
  764. static void bcm63xx_select_pullup(struct bcm63xx_udc *udc, bool is_on)
  765. {
  766. u32 val, portmask = BIT(udc->pd->port_no);
  767. val = bcm_rset_readl(RSET_USBH_PRIV, USBH_PRIV_UTMI_CTL_6368_REG);
  768. if (is_on)
  769. val &= ~(portmask << USBH_PRIV_UTMI_CTL_NODRIV_SHIFT);
  770. else
  771. val |= (portmask << USBH_PRIV_UTMI_CTL_NODRIV_SHIFT);
  772. bcm_rset_writel(RSET_USBH_PRIV, val, USBH_PRIV_UTMI_CTL_6368_REG);
  773. }
  774. /**
  775. * bcm63xx_uninit_udc_hw - Shut down the hardware prior to driver removal.
  776. * @udc: Reference to the device controller.
  777. *
  778. * This just masks the IUDMA IRQs and releases the clocks. It is assumed
  779. * that bcm63xx_udc_stop() has already run, and the clocks are stopped.
  780. */
  781. static void bcm63xx_uninit_udc_hw(struct bcm63xx_udc *udc)
  782. {
  783. set_clocks(udc, true);
  784. iudma_uninit(udc);
  785. set_clocks(udc, false);
  786. clk_put(udc->usbd_clk);
  787. clk_put(udc->usbh_clk);
  788. }
  789. /**
  790. * bcm63xx_init_udc_hw - Initialize the controller hardware and data structures.
  791. * @udc: Reference to the device controller.
  792. */
  793. static int bcm63xx_init_udc_hw(struct bcm63xx_udc *udc)
  794. {
  795. int i, rc = 0;
  796. u32 val;
  797. udc->ep0_ctrl_buf = devm_kzalloc(udc->dev, BCM63XX_MAX_CTRL_PKT,
  798. GFP_KERNEL);
  799. if (!udc->ep0_ctrl_buf)
  800. return -ENOMEM;
  801. INIT_LIST_HEAD(&udc->gadget.ep_list);
  802. for (i = 0; i < BCM63XX_NUM_EP; i++) {
  803. struct bcm63xx_ep *bep = &udc->bep[i];
  804. bep->ep.name = bcm63xx_ep_name[i];
  805. bep->ep_num = i;
  806. bep->ep.ops = &bcm63xx_udc_ep_ops;
  807. list_add_tail(&bep->ep.ep_list, &udc->gadget.ep_list);
  808. bep->halted = 0;
  809. bep->ep.maxpacket = BCM63XX_MAX_CTRL_PKT;
  810. bep->udc = udc;
  811. bep->ep.desc = NULL;
  812. INIT_LIST_HEAD(&bep->queue);
  813. }
  814. udc->gadget.ep0 = &udc->bep[0].ep;
  815. list_del(&udc->bep[0].ep.ep_list);
  816. udc->gadget.speed = USB_SPEED_UNKNOWN;
  817. udc->ep0state = EP0_SHUTDOWN;
  818. udc->usbh_clk = clk_get(udc->dev, "usbh");
  819. if (IS_ERR(udc->usbh_clk))
  820. return -EIO;
  821. udc->usbd_clk = clk_get(udc->dev, "usbd");
  822. if (IS_ERR(udc->usbd_clk)) {
  823. clk_put(udc->usbh_clk);
  824. return -EIO;
  825. }
  826. set_clocks(udc, true);
  827. val = USBD_CONTROL_AUTO_CSRS_MASK |
  828. USBD_CONTROL_DONE_CSRS_MASK |
  829. (irq_coalesce ? USBD_CONTROL_RXZSCFG_MASK : 0);
  830. usbd_writel(udc, val, USBD_CONTROL_REG);
  831. val = USBD_STRAPS_APP_SELF_PWR_MASK |
  832. USBD_STRAPS_APP_RAM_IF_MASK |
  833. USBD_STRAPS_APP_CSRPRGSUP_MASK |
  834. USBD_STRAPS_APP_8BITPHY_MASK |
  835. USBD_STRAPS_APP_RMTWKUP_MASK;
  836. if (udc->gadget.max_speed == USB_SPEED_HIGH)
  837. val |= (BCM63XX_SPD_HIGH << USBD_STRAPS_SPEED_SHIFT);
  838. else
  839. val |= (BCM63XX_SPD_FULL << USBD_STRAPS_SPEED_SHIFT);
  840. usbd_writel(udc, val, USBD_STRAPS_REG);
  841. bcm63xx_set_ctrl_irqs(udc, false);
  842. usbd_writel(udc, 0, USBD_EVENT_IRQ_CFG_LO_REG);
  843. val = USBD_EVENT_IRQ_CFG_FALLING(USBD_EVENT_IRQ_ENUM_ON) |
  844. USBD_EVENT_IRQ_CFG_FALLING(USBD_EVENT_IRQ_SET_CSRS);
  845. usbd_writel(udc, val, USBD_EVENT_IRQ_CFG_HI_REG);
  846. rc = iudma_init(udc);
  847. set_clocks(udc, false);
  848. if (rc)
  849. bcm63xx_uninit_udc_hw(udc);
  850. return 0;
  851. }
  852. /***********************************************************************
  853. * Standard EP gadget operations
  854. ***********************************************************************/
  855. /**
  856. * bcm63xx_ep_enable - Enable one endpoint.
  857. * @ep: Endpoint to enable.
  858. * @desc: Contains max packet, direction, etc.
  859. *
  860. * Most of the endpoint parameters are fixed in this controller, so there
  861. * isn't much for this function to do.
  862. */
  863. static int bcm63xx_ep_enable(struct usb_ep *ep,
  864. const struct usb_endpoint_descriptor *desc)
  865. {
  866. struct bcm63xx_ep *bep = our_ep(ep);
  867. struct bcm63xx_udc *udc = bep->udc;
  868. struct iudma_ch *iudma = bep->iudma;
  869. unsigned long flags;
  870. if (!ep || !desc || ep->name == bcm63xx_ep0name)
  871. return -EINVAL;
  872. if (!udc->driver)
  873. return -ESHUTDOWN;
  874. spin_lock_irqsave(&udc->lock, flags);
  875. if (iudma->enabled) {
  876. spin_unlock_irqrestore(&udc->lock, flags);
  877. return -EINVAL;
  878. }
  879. iudma->enabled = true;
  880. BUG_ON(!list_empty(&bep->queue));
  881. iudma_reset_channel(udc, iudma);
  882. bep->halted = 0;
  883. bcm63xx_set_stall(udc, bep, false);
  884. clear_bit(bep->ep_num, &udc->wedgemap);
  885. ep->desc = desc;
  886. ep->maxpacket = usb_endpoint_maxp(desc);
  887. spin_unlock_irqrestore(&udc->lock, flags);
  888. return 0;
  889. }
  890. /**
  891. * bcm63xx_ep_disable - Disable one endpoint.
  892. * @ep: Endpoint to disable.
  893. */
  894. static int bcm63xx_ep_disable(struct usb_ep *ep)
  895. {
  896. struct bcm63xx_ep *bep = our_ep(ep);
  897. struct bcm63xx_udc *udc = bep->udc;
  898. struct iudma_ch *iudma = bep->iudma;
  899. struct list_head *pos, *n;
  900. unsigned long flags;
  901. if (!ep || !ep->desc)
  902. return -EINVAL;
  903. spin_lock_irqsave(&udc->lock, flags);
  904. if (!iudma->enabled) {
  905. spin_unlock_irqrestore(&udc->lock, flags);
  906. return -EINVAL;
  907. }
  908. iudma->enabled = false;
  909. iudma_reset_channel(udc, iudma);
  910. if (!list_empty(&bep->queue)) {
  911. list_for_each_safe(pos, n, &bep->queue) {
  912. struct bcm63xx_req *breq =
  913. list_entry(pos, struct bcm63xx_req, queue);
  914. usb_gadget_unmap_request(&udc->gadget, &breq->req,
  915. iudma->is_tx);
  916. list_del(&breq->queue);
  917. breq->req.status = -ESHUTDOWN;
  918. spin_unlock_irqrestore(&udc->lock, flags);
  919. breq->req.complete(&iudma->bep->ep, &breq->req);
  920. spin_lock_irqsave(&udc->lock, flags);
  921. }
  922. }
  923. ep->desc = NULL;
  924. spin_unlock_irqrestore(&udc->lock, flags);
  925. return 0;
  926. }
  927. /**
  928. * bcm63xx_udc_alloc_request - Allocate a new request.
  929. * @ep: Endpoint associated with the request.
  930. * @mem_flags: Flags to pass to kzalloc().
  931. */
  932. static struct usb_request *bcm63xx_udc_alloc_request(struct usb_ep *ep,
  933. gfp_t mem_flags)
  934. {
  935. struct bcm63xx_req *breq;
  936. breq = kzalloc(sizeof(*breq), mem_flags);
  937. if (!breq)
  938. return NULL;
  939. return &breq->req;
  940. }
  941. /**
  942. * bcm63xx_udc_free_request - Free a request.
  943. * @ep: Endpoint associated with the request.
  944. * @req: Request to free.
  945. */
  946. static void bcm63xx_udc_free_request(struct usb_ep *ep,
  947. struct usb_request *req)
  948. {
  949. struct bcm63xx_req *breq = our_req(req);
  950. kfree(breq);
  951. }
  952. /**
  953. * bcm63xx_udc_queue - Queue up a new request.
  954. * @ep: Endpoint associated with the request.
  955. * @req: Request to add.
  956. * @mem_flags: Unused.
  957. *
  958. * If the queue is empty, start this request immediately. Otherwise, add
  959. * it to the list.
  960. *
  961. * ep0 replies are sent through this function from the gadget driver, but
  962. * they are treated differently because they need to be handled by the ep0
  963. * state machine. (Sometimes they are replies to control requests that
  964. * were spoofed by this driver, and so they shouldn't be transmitted at all.)
  965. */
  966. static int bcm63xx_udc_queue(struct usb_ep *ep, struct usb_request *req,
  967. gfp_t mem_flags)
  968. {
  969. struct bcm63xx_ep *bep = our_ep(ep);
  970. struct bcm63xx_udc *udc = bep->udc;
  971. struct bcm63xx_req *breq = our_req(req);
  972. unsigned long flags;
  973. int rc = 0;
  974. if (unlikely(!req || !req->complete || !req->buf || !ep))
  975. return -EINVAL;
  976. req->actual = 0;
  977. req->status = 0;
  978. breq->offset = 0;
  979. if (bep == &udc->bep[0]) {
  980. /* only one reply per request, please */
  981. if (udc->ep0_reply)
  982. return -EINVAL;
  983. udc->ep0_reply = req;
  984. schedule_work(&udc->ep0_wq);
  985. return 0;
  986. }
  987. spin_lock_irqsave(&udc->lock, flags);
  988. if (!bep->iudma->enabled) {
  989. rc = -ESHUTDOWN;
  990. goto out;
  991. }
  992. rc = usb_gadget_map_request(&udc->gadget, req, bep->iudma->is_tx);
  993. if (rc == 0) {
  994. list_add_tail(&breq->queue, &bep->queue);
  995. if (list_is_singular(&bep->queue))
  996. iudma_write(udc, bep->iudma, breq);
  997. }
  998. out:
  999. spin_unlock_irqrestore(&udc->lock, flags);
  1000. return rc;
  1001. }
  1002. /**
  1003. * bcm63xx_udc_dequeue - Remove a pending request from the queue.
  1004. * @ep: Endpoint associated with the request.
  1005. * @req: Request to remove.
  1006. *
  1007. * If the request is not at the head of the queue, this is easy - just nuke
  1008. * it. If the request is at the head of the queue, we'll need to stop the
  1009. * DMA transaction and then queue up the successor.
  1010. */
  1011. static int bcm63xx_udc_dequeue(struct usb_ep *ep, struct usb_request *req)
  1012. {
  1013. struct bcm63xx_ep *bep = our_ep(ep);
  1014. struct bcm63xx_udc *udc = bep->udc;
  1015. struct bcm63xx_req *breq = our_req(req), *cur;
  1016. unsigned long flags;
  1017. int rc = 0;
  1018. spin_lock_irqsave(&udc->lock, flags);
  1019. if (list_empty(&bep->queue)) {
  1020. rc = -EINVAL;
  1021. goto out;
  1022. }
  1023. cur = list_first_entry(&bep->queue, struct bcm63xx_req, queue);
  1024. usb_gadget_unmap_request(&udc->gadget, &breq->req, bep->iudma->is_tx);
  1025. if (breq == cur) {
  1026. iudma_reset_channel(udc, bep->iudma);
  1027. list_del(&breq->queue);
  1028. if (!list_empty(&bep->queue)) {
  1029. struct bcm63xx_req *next;
  1030. next = list_first_entry(&bep->queue,
  1031. struct bcm63xx_req, queue);
  1032. iudma_write(udc, bep->iudma, next);
  1033. }
  1034. } else {
  1035. list_del(&breq->queue);
  1036. }
  1037. out:
  1038. spin_unlock_irqrestore(&udc->lock, flags);
  1039. req->status = -ESHUTDOWN;
  1040. req->complete(ep, req);
  1041. return rc;
  1042. }
  1043. /**
  1044. * bcm63xx_udc_set_halt - Enable/disable STALL flag in the hardware.
  1045. * @ep: Endpoint to halt.
  1046. * @value: Zero to clear halt; nonzero to set halt.
  1047. *
  1048. * See comments in bcm63xx_update_wedge().
  1049. */
  1050. static int bcm63xx_udc_set_halt(struct usb_ep *ep, int value)
  1051. {
  1052. struct bcm63xx_ep *bep = our_ep(ep);
  1053. struct bcm63xx_udc *udc = bep->udc;
  1054. unsigned long flags;
  1055. spin_lock_irqsave(&udc->lock, flags);
  1056. bcm63xx_set_stall(udc, bep, !!value);
  1057. bep->halted = value;
  1058. spin_unlock_irqrestore(&udc->lock, flags);
  1059. return 0;
  1060. }
  1061. /**
  1062. * bcm63xx_udc_set_wedge - Stall the endpoint until the next reset.
  1063. * @ep: Endpoint to wedge.
  1064. *
  1065. * See comments in bcm63xx_update_wedge().
  1066. */
  1067. static int bcm63xx_udc_set_wedge(struct usb_ep *ep)
  1068. {
  1069. struct bcm63xx_ep *bep = our_ep(ep);
  1070. struct bcm63xx_udc *udc = bep->udc;
  1071. unsigned long flags;
  1072. spin_lock_irqsave(&udc->lock, flags);
  1073. set_bit(bep->ep_num, &udc->wedgemap);
  1074. bcm63xx_set_stall(udc, bep, true);
  1075. spin_unlock_irqrestore(&udc->lock, flags);
  1076. return 0;
  1077. }
  1078. static const struct usb_ep_ops bcm63xx_udc_ep_ops = {
  1079. .enable = bcm63xx_ep_enable,
  1080. .disable = bcm63xx_ep_disable,
  1081. .alloc_request = bcm63xx_udc_alloc_request,
  1082. .free_request = bcm63xx_udc_free_request,
  1083. .queue = bcm63xx_udc_queue,
  1084. .dequeue = bcm63xx_udc_dequeue,
  1085. .set_halt = bcm63xx_udc_set_halt,
  1086. .set_wedge = bcm63xx_udc_set_wedge,
  1087. };
  1088. /***********************************************************************
  1089. * EP0 handling
  1090. ***********************************************************************/
  1091. /**
  1092. * bcm63xx_ep0_setup_callback - Drop spinlock to invoke ->setup callback.
  1093. * @udc: Reference to the device controller.
  1094. * @ctrl: 8-byte SETUP request.
  1095. */
  1096. static int bcm63xx_ep0_setup_callback(struct bcm63xx_udc *udc,
  1097. struct usb_ctrlrequest *ctrl)
  1098. {
  1099. int rc;
  1100. spin_unlock_irq(&udc->lock);
  1101. rc = udc->driver->setup(&udc->gadget, ctrl);
  1102. spin_lock_irq(&udc->lock);
  1103. return rc;
  1104. }
  1105. /**
  1106. * bcm63xx_ep0_spoof_set_cfg - Synthesize a SET_CONFIGURATION request.
  1107. * @udc: Reference to the device controller.
  1108. *
  1109. * Many standard requests are handled automatically in the hardware, but
  1110. * we still need to pass them to the gadget driver so that it can
  1111. * reconfigure the interfaces/endpoints if necessary.
  1112. *
  1113. * Unfortunately we are not able to send a STALL response if the host
  1114. * requests an invalid configuration. If this happens, we'll have to be
  1115. * content with printing a warning.
  1116. */
  1117. static int bcm63xx_ep0_spoof_set_cfg(struct bcm63xx_udc *udc)
  1118. {
  1119. struct usb_ctrlrequest ctrl;
  1120. int rc;
  1121. ctrl.bRequestType = USB_DIR_OUT | USB_RECIP_DEVICE;
  1122. ctrl.bRequest = USB_REQ_SET_CONFIGURATION;
  1123. ctrl.wValue = cpu_to_le16(udc->cfg);
  1124. ctrl.wIndex = 0;
  1125. ctrl.wLength = 0;
  1126. rc = bcm63xx_ep0_setup_callback(udc, &ctrl);
  1127. if (rc < 0) {
  1128. dev_warn_ratelimited(udc->dev,
  1129. "hardware auto-acked bad SET_CONFIGURATION(%d) request\n",
  1130. udc->cfg);
  1131. }
  1132. return rc;
  1133. }
  1134. /**
  1135. * bcm63xx_ep0_spoof_set_iface - Synthesize a SET_INTERFACE request.
  1136. * @udc: Reference to the device controller.
  1137. */
  1138. static int bcm63xx_ep0_spoof_set_iface(struct bcm63xx_udc *udc)
  1139. {
  1140. struct usb_ctrlrequest ctrl;
  1141. int rc;
  1142. ctrl.bRequestType = USB_DIR_OUT | USB_RECIP_INTERFACE;
  1143. ctrl.bRequest = USB_REQ_SET_INTERFACE;
  1144. ctrl.wValue = cpu_to_le16(udc->alt_iface);
  1145. ctrl.wIndex = cpu_to_le16(udc->iface);
  1146. ctrl.wLength = 0;
  1147. rc = bcm63xx_ep0_setup_callback(udc, &ctrl);
  1148. if (rc < 0) {
  1149. dev_warn_ratelimited(udc->dev,
  1150. "hardware auto-acked bad SET_INTERFACE(%d,%d) request\n",
  1151. udc->iface, udc->alt_iface);
  1152. }
  1153. return rc;
  1154. }
  1155. /**
  1156. * bcm63xx_ep0_map_write - dma_map and iudma_write a single request.
  1157. * @udc: Reference to the device controller.
  1158. * @ch_idx: IUDMA channel number.
  1159. * @req: USB gadget layer representation of the request.
  1160. */
  1161. static void bcm63xx_ep0_map_write(struct bcm63xx_udc *udc, int ch_idx,
  1162. struct usb_request *req)
  1163. {
  1164. struct bcm63xx_req *breq = our_req(req);
  1165. struct iudma_ch *iudma = &udc->iudma[ch_idx];
  1166. BUG_ON(udc->ep0_request);
  1167. udc->ep0_request = req;
  1168. req->actual = 0;
  1169. breq->offset = 0;
  1170. usb_gadget_map_request(&udc->gadget, req, iudma->is_tx);
  1171. iudma_write(udc, iudma, breq);
  1172. }
  1173. /**
  1174. * bcm63xx_ep0_complete - Set completion status and "stage" the callback.
  1175. * @udc: Reference to the device controller.
  1176. * @req: USB gadget layer representation of the request.
  1177. * @status: Status to return to the gadget driver.
  1178. */
  1179. static void bcm63xx_ep0_complete(struct bcm63xx_udc *udc,
  1180. struct usb_request *req, int status)
  1181. {
  1182. req->status = status;
  1183. if (status)
  1184. req->actual = 0;
  1185. if (req->complete) {
  1186. spin_unlock_irq(&udc->lock);
  1187. req->complete(&udc->bep[0].ep, req);
  1188. spin_lock_irq(&udc->lock);
  1189. }
  1190. }
  1191. /**
  1192. * bcm63xx_ep0_nuke_reply - Abort request from the gadget driver due to
  1193. * reset/shutdown.
  1194. * @udc: Reference to the device controller.
  1195. * @is_tx: Nonzero for TX (IN), zero for RX (OUT).
  1196. */
  1197. static void bcm63xx_ep0_nuke_reply(struct bcm63xx_udc *udc, int is_tx)
  1198. {
  1199. struct usb_request *req = udc->ep0_reply;
  1200. udc->ep0_reply = NULL;
  1201. usb_gadget_unmap_request(&udc->gadget, req, is_tx);
  1202. if (udc->ep0_request == req) {
  1203. udc->ep0_req_completed = 0;
  1204. udc->ep0_request = NULL;
  1205. }
  1206. bcm63xx_ep0_complete(udc, req, -ESHUTDOWN);
  1207. }
  1208. /**
  1209. * bcm63xx_ep0_read_complete - Close out the pending ep0 request; return
  1210. * transfer len.
  1211. * @udc: Reference to the device controller.
  1212. */
  1213. static int bcm63xx_ep0_read_complete(struct bcm63xx_udc *udc)
  1214. {
  1215. struct usb_request *req = udc->ep0_request;
  1216. udc->ep0_req_completed = 0;
  1217. udc->ep0_request = NULL;
  1218. return req->actual;
  1219. }
  1220. /**
  1221. * bcm63xx_ep0_internal_request - Helper function to submit an ep0 request.
  1222. * @udc: Reference to the device controller.
  1223. * @ch_idx: IUDMA channel number.
  1224. * @length: Number of bytes to TX/RX.
  1225. *
  1226. * Used for simple transfers performed by the ep0 worker. This will always
  1227. * use ep0_ctrl_req / ep0_ctrl_buf.
  1228. */
  1229. static void bcm63xx_ep0_internal_request(struct bcm63xx_udc *udc, int ch_idx,
  1230. int length)
  1231. {
  1232. struct usb_request *req = &udc->ep0_ctrl_req.req;
  1233. req->buf = udc->ep0_ctrl_buf;
  1234. req->length = length;
  1235. req->complete = NULL;
  1236. bcm63xx_ep0_map_write(udc, ch_idx, req);
  1237. }
  1238. /**
  1239. * bcm63xx_ep0_do_setup - Parse new SETUP packet and decide how to handle it.
  1240. * @udc: Reference to the device controller.
  1241. *
  1242. * EP0_IDLE probably shouldn't ever happen. EP0_REQUEUE means we're ready
  1243. * for the next packet. Anything else means the transaction requires multiple
  1244. * stages of handling.
  1245. */
  1246. static enum bcm63xx_ep0_state bcm63xx_ep0_do_setup(struct bcm63xx_udc *udc)
  1247. {
  1248. int rc;
  1249. struct usb_ctrlrequest *ctrl = (void *)udc->ep0_ctrl_buf;
  1250. rc = bcm63xx_ep0_read_complete(udc);
  1251. if (rc < 0) {
  1252. dev_err(udc->dev, "missing SETUP packet\n");
  1253. return EP0_IDLE;
  1254. }
  1255. /*
  1256. * Handle 0-byte IN STATUS acknowledgement. The hardware doesn't
  1257. * ALWAYS deliver these 100% of the time, so if we happen to see one,
  1258. * just throw it away.
  1259. */
  1260. if (rc == 0)
  1261. return EP0_REQUEUE;
  1262. /* Drop malformed SETUP packets */
  1263. if (rc != sizeof(*ctrl)) {
  1264. dev_warn_ratelimited(udc->dev,
  1265. "malformed SETUP packet (%d bytes)\n", rc);
  1266. return EP0_REQUEUE;
  1267. }
  1268. /* Process new SETUP packet arriving on ep0 */
  1269. rc = bcm63xx_ep0_setup_callback(udc, ctrl);
  1270. if (rc < 0) {
  1271. bcm63xx_set_stall(udc, &udc->bep[0], true);
  1272. return EP0_REQUEUE;
  1273. }
  1274. if (!ctrl->wLength)
  1275. return EP0_REQUEUE;
  1276. else if (ctrl->bRequestType & USB_DIR_IN)
  1277. return EP0_IN_DATA_PHASE_SETUP;
  1278. else
  1279. return EP0_OUT_DATA_PHASE_SETUP;
  1280. }
  1281. /**
  1282. * bcm63xx_ep0_do_idle - Check for outstanding requests if ep0 is idle.
  1283. * @udc: Reference to the device controller.
  1284. *
  1285. * In state EP0_IDLE, the RX descriptor is either pending, or has been
  1286. * filled with a SETUP packet from the host. This function handles new
  1287. * SETUP packets, control IRQ events (which can generate fake SETUP packets),
  1288. * and reset/shutdown events.
  1289. *
  1290. * Returns 0 if work was done; -EAGAIN if nothing to do.
  1291. */
  1292. static int bcm63xx_ep0_do_idle(struct bcm63xx_udc *udc)
  1293. {
  1294. if (udc->ep0_req_reset) {
  1295. udc->ep0_req_reset = 0;
  1296. } else if (udc->ep0_req_set_cfg) {
  1297. udc->ep0_req_set_cfg = 0;
  1298. if (bcm63xx_ep0_spoof_set_cfg(udc) >= 0)
  1299. udc->ep0state = EP0_IN_FAKE_STATUS_PHASE;
  1300. } else if (udc->ep0_req_set_iface) {
  1301. udc->ep0_req_set_iface = 0;
  1302. if (bcm63xx_ep0_spoof_set_iface(udc) >= 0)
  1303. udc->ep0state = EP0_IN_FAKE_STATUS_PHASE;
  1304. } else if (udc->ep0_req_completed) {
  1305. udc->ep0state = bcm63xx_ep0_do_setup(udc);
  1306. return udc->ep0state == EP0_IDLE ? -EAGAIN : 0;
  1307. } else if (udc->ep0_req_shutdown) {
  1308. udc->ep0_req_shutdown = 0;
  1309. udc->ep0_req_completed = 0;
  1310. udc->ep0_request = NULL;
  1311. iudma_reset_channel(udc, &udc->iudma[IUDMA_EP0_RXCHAN]);
  1312. usb_gadget_unmap_request(&udc->gadget,
  1313. &udc->ep0_ctrl_req.req, 0);
  1314. /* bcm63xx_udc_pullup() is waiting for this */
  1315. mb();
  1316. udc->ep0state = EP0_SHUTDOWN;
  1317. } else if (udc->ep0_reply) {
  1318. /*
  1319. * This could happen if a USB RESET shows up during an ep0
  1320. * transaction (especially if a laggy driver like gadgetfs
  1321. * is in use).
  1322. */
  1323. dev_warn(udc->dev, "nuking unexpected reply\n");
  1324. bcm63xx_ep0_nuke_reply(udc, 0);
  1325. } else {
  1326. return -EAGAIN;
  1327. }
  1328. return 0;
  1329. }
  1330. /**
  1331. * bcm63xx_ep0_one_round - Handle the current ep0 state.
  1332. * @udc: Reference to the device controller.
  1333. *
  1334. * Returns 0 if work was done; -EAGAIN if nothing to do.
  1335. */
  1336. static int bcm63xx_ep0_one_round(struct bcm63xx_udc *udc)
  1337. {
  1338. enum bcm63xx_ep0_state ep0state = udc->ep0state;
  1339. bool shutdown = udc->ep0_req_reset || udc->ep0_req_shutdown;
  1340. switch (udc->ep0state) {
  1341. case EP0_REQUEUE:
  1342. /* set up descriptor to receive SETUP packet */
  1343. bcm63xx_ep0_internal_request(udc, IUDMA_EP0_RXCHAN,
  1344. BCM63XX_MAX_CTRL_PKT);
  1345. ep0state = EP0_IDLE;
  1346. break;
  1347. case EP0_IDLE:
  1348. return bcm63xx_ep0_do_idle(udc);
  1349. case EP0_IN_DATA_PHASE_SETUP:
  1350. /*
  1351. * Normal case: TX request is in ep0_reply (queued by the
  1352. * callback), or will be queued shortly. When it's here,
  1353. * send it to the HW and go to EP0_IN_DATA_PHASE_COMPLETE.
  1354. *
  1355. * Shutdown case: Stop waiting for the reply. Just
  1356. * REQUEUE->IDLE. The gadget driver is NOT expected to
  1357. * queue anything else now.
  1358. */
  1359. if (udc->ep0_reply) {
  1360. bcm63xx_ep0_map_write(udc, IUDMA_EP0_TXCHAN,
  1361. udc->ep0_reply);
  1362. ep0state = EP0_IN_DATA_PHASE_COMPLETE;
  1363. } else if (shutdown) {
  1364. ep0state = EP0_REQUEUE;
  1365. }
  1366. break;
  1367. case EP0_IN_DATA_PHASE_COMPLETE: {
  1368. /*
  1369. * Normal case: TX packet (ep0_reply) is in flight; wait for
  1370. * it to finish, then go back to REQUEUE->IDLE.
  1371. *
  1372. * Shutdown case: Reset the TX channel, send -ESHUTDOWN
  1373. * completion to the gadget driver, then REQUEUE->IDLE.
  1374. */
  1375. if (udc->ep0_req_completed) {
  1376. udc->ep0_reply = NULL;
  1377. bcm63xx_ep0_read_complete(udc);
  1378. /*
  1379. * the "ack" sometimes gets eaten (see
  1380. * bcm63xx_ep0_do_idle)
  1381. */
  1382. ep0state = EP0_REQUEUE;
  1383. } else if (shutdown) {
  1384. iudma_reset_channel(udc, &udc->iudma[IUDMA_EP0_TXCHAN]);
  1385. bcm63xx_ep0_nuke_reply(udc, 1);
  1386. ep0state = EP0_REQUEUE;
  1387. }
  1388. break;
  1389. }
  1390. case EP0_OUT_DATA_PHASE_SETUP:
  1391. /* Similar behavior to EP0_IN_DATA_PHASE_SETUP */
  1392. if (udc->ep0_reply) {
  1393. bcm63xx_ep0_map_write(udc, IUDMA_EP0_RXCHAN,
  1394. udc->ep0_reply);
  1395. ep0state = EP0_OUT_DATA_PHASE_COMPLETE;
  1396. } else if (shutdown) {
  1397. ep0state = EP0_REQUEUE;
  1398. }
  1399. break;
  1400. case EP0_OUT_DATA_PHASE_COMPLETE: {
  1401. /* Similar behavior to EP0_IN_DATA_PHASE_COMPLETE */
  1402. if (udc->ep0_req_completed) {
  1403. udc->ep0_reply = NULL;
  1404. bcm63xx_ep0_read_complete(udc);
  1405. /* send 0-byte ack to host */
  1406. bcm63xx_ep0_internal_request(udc, IUDMA_EP0_TXCHAN, 0);
  1407. ep0state = EP0_OUT_STATUS_PHASE;
  1408. } else if (shutdown) {
  1409. iudma_reset_channel(udc, &udc->iudma[IUDMA_EP0_RXCHAN]);
  1410. bcm63xx_ep0_nuke_reply(udc, 0);
  1411. ep0state = EP0_REQUEUE;
  1412. }
  1413. break;
  1414. }
  1415. case EP0_OUT_STATUS_PHASE:
  1416. /*
  1417. * Normal case: 0-byte OUT ack packet is in flight; wait
  1418. * for it to finish, then go back to REQUEUE->IDLE.
  1419. *
  1420. * Shutdown case: just cancel the transmission. Don't bother
  1421. * calling the completion, because it originated from this
  1422. * function anyway. Then go back to REQUEUE->IDLE.
  1423. */
  1424. if (udc->ep0_req_completed) {
  1425. bcm63xx_ep0_read_complete(udc);
  1426. ep0state = EP0_REQUEUE;
  1427. } else if (shutdown) {
  1428. iudma_reset_channel(udc, &udc->iudma[IUDMA_EP0_TXCHAN]);
  1429. udc->ep0_request = NULL;
  1430. ep0state = EP0_REQUEUE;
  1431. }
  1432. break;
  1433. case EP0_IN_FAKE_STATUS_PHASE: {
  1434. /*
  1435. * Normal case: we spoofed a SETUP packet and are now
  1436. * waiting for the gadget driver to send a 0-byte reply.
  1437. * This doesn't actually get sent to the HW because the
  1438. * HW has already sent its own reply. Once we get the
  1439. * response, return to IDLE.
  1440. *
  1441. * Shutdown case: return to IDLE immediately.
  1442. *
  1443. * Note that the ep0 RX descriptor has remained queued
  1444. * (and possibly unfilled) during this entire transaction.
  1445. * The HW datapath (IUDMA) never even sees SET_CONFIGURATION
  1446. * or SET_INTERFACE transactions.
  1447. */
  1448. struct usb_request *r = udc->ep0_reply;
  1449. if (!r) {
  1450. if (shutdown)
  1451. ep0state = EP0_IDLE;
  1452. break;
  1453. }
  1454. bcm63xx_ep0_complete(udc, r, 0);
  1455. udc->ep0_reply = NULL;
  1456. ep0state = EP0_IDLE;
  1457. break;
  1458. }
  1459. case EP0_SHUTDOWN:
  1460. break;
  1461. }
  1462. if (udc->ep0state == ep0state)
  1463. return -EAGAIN;
  1464. udc->ep0state = ep0state;
  1465. return 0;
  1466. }
  1467. /**
  1468. * bcm63xx_ep0_process - ep0 worker thread / state machine.
  1469. * @w: Workqueue struct.
  1470. *
  1471. * bcm63xx_ep0_process is triggered any time an event occurs on ep0. It
  1472. * is used to synchronize ep0 events and ensure that both HW and SW events
  1473. * occur in a well-defined order. When the ep0 IUDMA queues are idle, it may
  1474. * synthesize SET_CONFIGURATION / SET_INTERFACE requests that were consumed
  1475. * by the USBD hardware.
  1476. *
  1477. * The worker function will continue iterating around the state machine
  1478. * until there is nothing left to do. Usually "nothing left to do" means
  1479. * that we're waiting for a new event from the hardware.
  1480. */
  1481. static void bcm63xx_ep0_process(struct work_struct *w)
  1482. {
  1483. struct bcm63xx_udc *udc = container_of(w, struct bcm63xx_udc, ep0_wq);
  1484. spin_lock_irq(&udc->lock);
  1485. while (bcm63xx_ep0_one_round(udc) == 0)
  1486. ;
  1487. spin_unlock_irq(&udc->lock);
  1488. }
  1489. /***********************************************************************
  1490. * Standard UDC gadget operations
  1491. ***********************************************************************/
  1492. /**
  1493. * bcm63xx_udc_get_frame - Read current SOF frame number from the HW.
  1494. * @gadget: USB slave device.
  1495. */
  1496. static int bcm63xx_udc_get_frame(struct usb_gadget *gadget)
  1497. {
  1498. struct bcm63xx_udc *udc = gadget_to_udc(gadget);
  1499. return (usbd_readl(udc, USBD_STATUS_REG) &
  1500. USBD_STATUS_SOF_MASK) >> USBD_STATUS_SOF_SHIFT;
  1501. }
  1502. /**
  1503. * bcm63xx_udc_pullup - Enable/disable pullup on D+ line.
  1504. * @gadget: USB slave device.
  1505. * @is_on: 0 to disable pullup, 1 to enable.
  1506. *
  1507. * See notes in bcm63xx_select_pullup().
  1508. */
  1509. static int bcm63xx_udc_pullup(struct usb_gadget *gadget, int is_on)
  1510. {
  1511. struct bcm63xx_udc *udc = gadget_to_udc(gadget);
  1512. unsigned long flags;
  1513. int i, rc = -EINVAL;
  1514. spin_lock_irqsave(&udc->lock, flags);
  1515. if (is_on && udc->ep0state == EP0_SHUTDOWN) {
  1516. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1517. udc->ep0state = EP0_REQUEUE;
  1518. bcm63xx_fifo_setup(udc);
  1519. bcm63xx_fifo_reset(udc);
  1520. bcm63xx_ep_setup(udc);
  1521. bitmap_zero(&udc->wedgemap, BCM63XX_NUM_EP);
  1522. for (i = 0; i < BCM63XX_NUM_EP; i++)
  1523. bcm63xx_set_stall(udc, &udc->bep[i], false);
  1524. bcm63xx_set_ctrl_irqs(udc, true);
  1525. bcm63xx_select_pullup(gadget_to_udc(gadget), true);
  1526. rc = 0;
  1527. } else if (!is_on && udc->ep0state != EP0_SHUTDOWN) {
  1528. bcm63xx_select_pullup(gadget_to_udc(gadget), false);
  1529. udc->ep0_req_shutdown = 1;
  1530. spin_unlock_irqrestore(&udc->lock, flags);
  1531. while (1) {
  1532. schedule_work(&udc->ep0_wq);
  1533. if (udc->ep0state == EP0_SHUTDOWN)
  1534. break;
  1535. msleep(50);
  1536. }
  1537. bcm63xx_set_ctrl_irqs(udc, false);
  1538. cancel_work_sync(&udc->ep0_wq);
  1539. return 0;
  1540. }
  1541. spin_unlock_irqrestore(&udc->lock, flags);
  1542. return rc;
  1543. }
  1544. /**
  1545. * bcm63xx_udc_start - Start the controller.
  1546. * @gadget: USB slave device.
  1547. * @driver: Driver for USB slave devices.
  1548. */
  1549. static int bcm63xx_udc_start(struct usb_gadget *gadget,
  1550. struct usb_gadget_driver *driver)
  1551. {
  1552. struct bcm63xx_udc *udc = gadget_to_udc(gadget);
  1553. unsigned long flags;
  1554. if (!driver || driver->max_speed < USB_SPEED_HIGH ||
  1555. !driver->setup)
  1556. return -EINVAL;
  1557. if (!udc)
  1558. return -ENODEV;
  1559. if (udc->driver)
  1560. return -EBUSY;
  1561. spin_lock_irqsave(&udc->lock, flags);
  1562. set_clocks(udc, true);
  1563. bcm63xx_fifo_setup(udc);
  1564. bcm63xx_ep_init(udc);
  1565. bcm63xx_ep_setup(udc);
  1566. bcm63xx_fifo_reset(udc);
  1567. bcm63xx_select_phy_mode(udc, true);
  1568. udc->driver = driver;
  1569. driver->driver.bus = NULL;
  1570. udc->gadget.dev.driver = &driver->driver;
  1571. udc->gadget.dev.of_node = udc->dev->of_node;
  1572. spin_unlock_irqrestore(&udc->lock, flags);
  1573. return 0;
  1574. }
  1575. /**
  1576. * bcm63xx_udc_stop - Shut down the controller.
  1577. * @gadget: USB slave device.
  1578. * @driver: Driver for USB slave devices.
  1579. */
  1580. static int bcm63xx_udc_stop(struct usb_gadget *gadget,
  1581. struct usb_gadget_driver *driver)
  1582. {
  1583. struct bcm63xx_udc *udc = gadget_to_udc(gadget);
  1584. unsigned long flags;
  1585. spin_lock_irqsave(&udc->lock, flags);
  1586. udc->driver = NULL;
  1587. udc->gadget.dev.driver = NULL;
  1588. /*
  1589. * If we switch the PHY too abruptly after dropping D+, the host
  1590. * will often complain:
  1591. *
  1592. * hub 1-0:1.0: port 1 disabled by hub (EMI?), re-enabling...
  1593. */
  1594. msleep(100);
  1595. bcm63xx_select_phy_mode(udc, false);
  1596. set_clocks(udc, false);
  1597. spin_unlock_irqrestore(&udc->lock, flags);
  1598. return 0;
  1599. }
  1600. static const struct usb_gadget_ops bcm63xx_udc_ops = {
  1601. .get_frame = bcm63xx_udc_get_frame,
  1602. .pullup = bcm63xx_udc_pullup,
  1603. .udc_start = bcm63xx_udc_start,
  1604. .udc_stop = bcm63xx_udc_stop,
  1605. };
  1606. /***********************************************************************
  1607. * IRQ handling
  1608. ***********************************************************************/
  1609. /**
  1610. * bcm63xx_update_cfg_iface - Read current configuration/interface settings.
  1611. * @udc: Reference to the device controller.
  1612. *
  1613. * This controller intercepts SET_CONFIGURATION and SET_INTERFACE messages.
  1614. * The driver never sees the raw control packets coming in on the ep0
  1615. * IUDMA channel, but at least we get an interrupt event to tell us that
  1616. * new values are waiting in the USBD_STATUS register.
  1617. */
  1618. static void bcm63xx_update_cfg_iface(struct bcm63xx_udc *udc)
  1619. {
  1620. u32 reg = usbd_readl(udc, USBD_STATUS_REG);
  1621. udc->cfg = (reg & USBD_STATUS_CFG_MASK) >> USBD_STATUS_CFG_SHIFT;
  1622. udc->iface = (reg & USBD_STATUS_INTF_MASK) >> USBD_STATUS_INTF_SHIFT;
  1623. udc->alt_iface = (reg & USBD_STATUS_ALTINTF_MASK) >>
  1624. USBD_STATUS_ALTINTF_SHIFT;
  1625. bcm63xx_ep_setup(udc);
  1626. }
  1627. /**
  1628. * bcm63xx_update_link_speed - Check to see if the link speed has changed.
  1629. * @udc: Reference to the device controller.
  1630. *
  1631. * The link speed update coincides with a SETUP IRQ. Returns 1 if the
  1632. * speed has changed, so that the caller can update the endpoint settings.
  1633. */
  1634. static int bcm63xx_update_link_speed(struct bcm63xx_udc *udc)
  1635. {
  1636. u32 reg = usbd_readl(udc, USBD_STATUS_REG);
  1637. enum usb_device_speed oldspeed = udc->gadget.speed;
  1638. switch ((reg & USBD_STATUS_SPD_MASK) >> USBD_STATUS_SPD_SHIFT) {
  1639. case BCM63XX_SPD_HIGH:
  1640. udc->gadget.speed = USB_SPEED_HIGH;
  1641. break;
  1642. case BCM63XX_SPD_FULL:
  1643. udc->gadget.speed = USB_SPEED_FULL;
  1644. break;
  1645. default:
  1646. /* this should never happen */
  1647. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1648. dev_err(udc->dev,
  1649. "received SETUP packet with invalid link speed\n");
  1650. return 0;
  1651. }
  1652. if (udc->gadget.speed != oldspeed) {
  1653. dev_info(udc->dev, "link up, %s-speed mode\n",
  1654. udc->gadget.speed == USB_SPEED_HIGH ? "high" : "full");
  1655. return 1;
  1656. } else {
  1657. return 0;
  1658. }
  1659. }
  1660. /**
  1661. * bcm63xx_update_wedge - Iterate through wedged endpoints.
  1662. * @udc: Reference to the device controller.
  1663. * @new_status: true to "refresh" wedge status; false to clear it.
  1664. *
  1665. * On a SETUP interrupt, we need to manually "refresh" the wedge status
  1666. * because the controller hardware is designed to automatically clear
  1667. * stalls in response to a CLEAR_FEATURE request from the host.
  1668. *
  1669. * On a RESET interrupt, we do want to restore all wedged endpoints.
  1670. */
  1671. static void bcm63xx_update_wedge(struct bcm63xx_udc *udc, bool new_status)
  1672. {
  1673. int i;
  1674. for_each_set_bit(i, &udc->wedgemap, BCM63XX_NUM_EP) {
  1675. bcm63xx_set_stall(udc, &udc->bep[i], new_status);
  1676. if (!new_status)
  1677. clear_bit(i, &udc->wedgemap);
  1678. }
  1679. }
  1680. /**
  1681. * bcm63xx_udc_ctrl_isr - ISR for control path events (USBD).
  1682. * @irq: IRQ number (unused).
  1683. * @dev_id: Reference to the device controller.
  1684. *
  1685. * This is where we handle link (VBUS) down, USB reset, speed changes,
  1686. * SET_CONFIGURATION, and SET_INTERFACE events.
  1687. */
  1688. static irqreturn_t bcm63xx_udc_ctrl_isr(int irq, void *dev_id)
  1689. {
  1690. struct bcm63xx_udc *udc = dev_id;
  1691. u32 stat;
  1692. bool disconnected = false;
  1693. stat = usbd_readl(udc, USBD_EVENT_IRQ_STATUS_REG) &
  1694. usbd_readl(udc, USBD_EVENT_IRQ_MASK_REG);
  1695. usbd_writel(udc, stat, USBD_EVENT_IRQ_STATUS_REG);
  1696. spin_lock(&udc->lock);
  1697. if (stat & BIT(USBD_EVENT_IRQ_USB_LINK)) {
  1698. /* VBUS toggled */
  1699. if (!(usbd_readl(udc, USBD_EVENTS_REG) &
  1700. USBD_EVENTS_USB_LINK_MASK) &&
  1701. udc->gadget.speed != USB_SPEED_UNKNOWN)
  1702. dev_info(udc->dev, "link down\n");
  1703. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1704. disconnected = true;
  1705. }
  1706. if (stat & BIT(USBD_EVENT_IRQ_USB_RESET)) {
  1707. bcm63xx_fifo_setup(udc);
  1708. bcm63xx_fifo_reset(udc);
  1709. bcm63xx_ep_setup(udc);
  1710. bcm63xx_update_wedge(udc, false);
  1711. udc->ep0_req_reset = 1;
  1712. schedule_work(&udc->ep0_wq);
  1713. disconnected = true;
  1714. }
  1715. if (stat & BIT(USBD_EVENT_IRQ_SETUP)) {
  1716. if (bcm63xx_update_link_speed(udc)) {
  1717. bcm63xx_fifo_setup(udc);
  1718. bcm63xx_ep_setup(udc);
  1719. }
  1720. bcm63xx_update_wedge(udc, true);
  1721. }
  1722. if (stat & BIT(USBD_EVENT_IRQ_SETCFG)) {
  1723. bcm63xx_update_cfg_iface(udc);
  1724. udc->ep0_req_set_cfg = 1;
  1725. schedule_work(&udc->ep0_wq);
  1726. }
  1727. if (stat & BIT(USBD_EVENT_IRQ_SETINTF)) {
  1728. bcm63xx_update_cfg_iface(udc);
  1729. udc->ep0_req_set_iface = 1;
  1730. schedule_work(&udc->ep0_wq);
  1731. }
  1732. spin_unlock(&udc->lock);
  1733. if (disconnected && udc->driver)
  1734. udc->driver->disconnect(&udc->gadget);
  1735. return IRQ_HANDLED;
  1736. }
  1737. /**
  1738. * bcm63xx_udc_data_isr - ISR for data path events (IUDMA).
  1739. * @irq: IRQ number (unused).
  1740. * @dev_id: Reference to the IUDMA channel that generated the interrupt.
  1741. *
  1742. * For the two ep0 channels, we have special handling that triggers the
  1743. * ep0 worker thread. For normal bulk/intr channels, either queue up
  1744. * the next buffer descriptor for the transaction (incomplete transaction),
  1745. * or invoke the completion callback (complete transactions).
  1746. */
  1747. static irqreturn_t bcm63xx_udc_data_isr(int irq, void *dev_id)
  1748. {
  1749. struct iudma_ch *iudma = dev_id;
  1750. struct bcm63xx_udc *udc = iudma->udc;
  1751. struct bcm63xx_ep *bep;
  1752. struct usb_request *req = NULL;
  1753. struct bcm63xx_req *breq = NULL;
  1754. int rc;
  1755. bool is_done = false;
  1756. spin_lock(&udc->lock);
  1757. usb_dmac_writel(udc, ENETDMAC_IR_BUFDONE_MASK,
  1758. ENETDMAC_IR_REG(iudma->ch_idx));
  1759. bep = iudma->bep;
  1760. rc = iudma_read(udc, iudma);
  1761. /* special handling for EP0 RX (0) and TX (1) */
  1762. if (iudma->ch_idx == IUDMA_EP0_RXCHAN ||
  1763. iudma->ch_idx == IUDMA_EP0_TXCHAN) {
  1764. req = udc->ep0_request;
  1765. breq = our_req(req);
  1766. /* a single request could require multiple submissions */
  1767. if (rc >= 0) {
  1768. req->actual += rc;
  1769. if (req->actual >= req->length || breq->bd_bytes > rc) {
  1770. udc->ep0_req_completed = 1;
  1771. is_done = true;
  1772. schedule_work(&udc->ep0_wq);
  1773. /* "actual" on a ZLP is 1 byte */
  1774. req->actual = min(req->actual, req->length);
  1775. } else {
  1776. /* queue up the next BD (same request) */
  1777. iudma_write(udc, iudma, breq);
  1778. }
  1779. }
  1780. } else if (!list_empty(&bep->queue)) {
  1781. breq = list_first_entry(&bep->queue, struct bcm63xx_req, queue);
  1782. req = &breq->req;
  1783. if (rc >= 0) {
  1784. req->actual += rc;
  1785. if (req->actual >= req->length || breq->bd_bytes > rc) {
  1786. is_done = true;
  1787. list_del(&breq->queue);
  1788. req->actual = min(req->actual, req->length);
  1789. if (!list_empty(&bep->queue)) {
  1790. struct bcm63xx_req *next;
  1791. next = list_first_entry(&bep->queue,
  1792. struct bcm63xx_req, queue);
  1793. iudma_write(udc, iudma, next);
  1794. }
  1795. } else {
  1796. iudma_write(udc, iudma, breq);
  1797. }
  1798. }
  1799. }
  1800. spin_unlock(&udc->lock);
  1801. if (is_done) {
  1802. usb_gadget_unmap_request(&udc->gadget, req, iudma->is_tx);
  1803. if (req->complete)
  1804. req->complete(&bep->ep, req);
  1805. }
  1806. return IRQ_HANDLED;
  1807. }
  1808. /***********************************************************************
  1809. * Debug filesystem
  1810. ***********************************************************************/
  1811. /*
  1812. * bcm63xx_usbd_dbg_show - Show USBD controller state.
  1813. * @s: seq_file to which the information will be written.
  1814. * @p: Unused.
  1815. *
  1816. * This file nominally shows up as /sys/kernel/debug/bcm63xx_udc/usbd
  1817. */
  1818. static int bcm63xx_usbd_dbg_show(struct seq_file *s, void *p)
  1819. {
  1820. struct bcm63xx_udc *udc = s->private;
  1821. if (!udc->driver)
  1822. return -ENODEV;
  1823. seq_printf(s, "ep0 state: %s\n",
  1824. bcm63xx_ep0_state_names[udc->ep0state]);
  1825. seq_printf(s, " pending requests: %s%s%s%s%s%s%s\n",
  1826. udc->ep0_req_reset ? "reset " : "",
  1827. udc->ep0_req_set_cfg ? "set_cfg " : "",
  1828. udc->ep0_req_set_iface ? "set_iface " : "",
  1829. udc->ep0_req_shutdown ? "shutdown " : "",
  1830. udc->ep0_request ? "pending " : "",
  1831. udc->ep0_req_completed ? "completed " : "",
  1832. udc->ep0_reply ? "reply " : "");
  1833. seq_printf(s, "cfg: %d; iface: %d; alt_iface: %d\n",
  1834. udc->cfg, udc->iface, udc->alt_iface);
  1835. seq_printf(s, "regs:\n");
  1836. seq_printf(s, " control: %08x; straps: %08x; status: %08x\n",
  1837. usbd_readl(udc, USBD_CONTROL_REG),
  1838. usbd_readl(udc, USBD_STRAPS_REG),
  1839. usbd_readl(udc, USBD_STATUS_REG));
  1840. seq_printf(s, " events: %08x; stall: %08x\n",
  1841. usbd_readl(udc, USBD_EVENTS_REG),
  1842. usbd_readl(udc, USBD_STALL_REG));
  1843. return 0;
  1844. }
  1845. /*
  1846. * bcm63xx_iudma_dbg_show - Show IUDMA status and descriptors.
  1847. * @s: seq_file to which the information will be written.
  1848. * @p: Unused.
  1849. *
  1850. * This file nominally shows up as /sys/kernel/debug/bcm63xx_udc/iudma
  1851. */
  1852. static int bcm63xx_iudma_dbg_show(struct seq_file *s, void *p)
  1853. {
  1854. struct bcm63xx_udc *udc = s->private;
  1855. int ch_idx, i;
  1856. u32 sram2, sram3;
  1857. if (!udc->driver)
  1858. return -ENODEV;
  1859. for (ch_idx = 0; ch_idx < BCM63XX_NUM_IUDMA; ch_idx++) {
  1860. struct iudma_ch *iudma = &udc->iudma[ch_idx];
  1861. struct list_head *pos;
  1862. seq_printf(s, "IUDMA channel %d -- ", ch_idx);
  1863. switch (iudma_defaults[ch_idx].ep_type) {
  1864. case BCMEP_CTRL:
  1865. seq_printf(s, "control");
  1866. break;
  1867. case BCMEP_BULK:
  1868. seq_printf(s, "bulk");
  1869. break;
  1870. case BCMEP_INTR:
  1871. seq_printf(s, "interrupt");
  1872. break;
  1873. }
  1874. seq_printf(s, ch_idx & 0x01 ? " tx" : " rx");
  1875. seq_printf(s, " [ep%d]:\n",
  1876. max_t(int, iudma_defaults[ch_idx].ep_num, 0));
  1877. seq_printf(s, " cfg: %08x; irqstat: %08x; irqmask: %08x; maxburst: %08x\n",
  1878. usb_dmac_readl(udc, ENETDMAC_CHANCFG_REG(ch_idx)),
  1879. usb_dmac_readl(udc, ENETDMAC_IR_REG(ch_idx)),
  1880. usb_dmac_readl(udc, ENETDMAC_IRMASK_REG(ch_idx)),
  1881. usb_dmac_readl(udc, ENETDMAC_MAXBURST_REG(ch_idx)));
  1882. sram2 = usb_dmas_readl(udc, ENETDMAS_SRAM2_REG(ch_idx));
  1883. sram3 = usb_dmas_readl(udc, ENETDMAS_SRAM3_REG(ch_idx));
  1884. seq_printf(s, " base: %08x; index: %04x_%04x; desc: %04x_%04x %08x\n",
  1885. usb_dmas_readl(udc, ENETDMAS_RSTART_REG(ch_idx)),
  1886. sram2 >> 16, sram2 & 0xffff,
  1887. sram3 >> 16, sram3 & 0xffff,
  1888. usb_dmas_readl(udc, ENETDMAS_SRAM4_REG(ch_idx)));
  1889. seq_printf(s, " desc: %d/%d used", iudma->n_bds_used,
  1890. iudma->n_bds);
  1891. if (iudma->bep) {
  1892. i = 0;
  1893. list_for_each(pos, &iudma->bep->queue)
  1894. i++;
  1895. seq_printf(s, "; %d queued\n", i);
  1896. } else {
  1897. seq_printf(s, "\n");
  1898. }
  1899. for (i = 0; i < iudma->n_bds; i++) {
  1900. struct bcm_enet_desc *d = &iudma->bd_ring[i];
  1901. seq_printf(s, " %03x (%02x): len_stat: %04x_%04x; pa %08x",
  1902. i * sizeof(*d), i,
  1903. d->len_stat >> 16, d->len_stat & 0xffff,
  1904. d->address);
  1905. if (d == iudma->read_bd)
  1906. seq_printf(s, " <<RD");
  1907. if (d == iudma->write_bd)
  1908. seq_printf(s, " <<WR");
  1909. seq_printf(s, "\n");
  1910. }
  1911. seq_printf(s, "\n");
  1912. }
  1913. return 0;
  1914. }
  1915. static int bcm63xx_usbd_dbg_open(struct inode *inode, struct file *file)
  1916. {
  1917. return single_open(file, bcm63xx_usbd_dbg_show, inode->i_private);
  1918. }
  1919. static int bcm63xx_iudma_dbg_open(struct inode *inode, struct file *file)
  1920. {
  1921. return single_open(file, bcm63xx_iudma_dbg_show, inode->i_private);
  1922. }
  1923. static const struct file_operations usbd_dbg_fops = {
  1924. .owner = THIS_MODULE,
  1925. .open = bcm63xx_usbd_dbg_open,
  1926. .llseek = seq_lseek,
  1927. .read = seq_read,
  1928. .release = single_release,
  1929. };
  1930. static const struct file_operations iudma_dbg_fops = {
  1931. .owner = THIS_MODULE,
  1932. .open = bcm63xx_iudma_dbg_open,
  1933. .llseek = seq_lseek,
  1934. .read = seq_read,
  1935. .release = single_release,
  1936. };
  1937. /**
  1938. * bcm63xx_udc_init_debugfs - Create debugfs entries.
  1939. * @udc: Reference to the device controller.
  1940. */
  1941. static void bcm63xx_udc_init_debugfs(struct bcm63xx_udc *udc)
  1942. {
  1943. struct dentry *root, *usbd, *iudma;
  1944. if (!IS_ENABLED(CONFIG_USB_GADGET_DEBUG_FS))
  1945. return;
  1946. root = debugfs_create_dir(udc->gadget.name, NULL);
  1947. if (IS_ERR(root) || !root)
  1948. goto err_root;
  1949. usbd = debugfs_create_file("usbd", 0400, root, udc,
  1950. &usbd_dbg_fops);
  1951. if (!usbd)
  1952. goto err_usbd;
  1953. iudma = debugfs_create_file("iudma", 0400, root, udc,
  1954. &iudma_dbg_fops);
  1955. if (!iudma)
  1956. goto err_iudma;
  1957. udc->debugfs_root = root;
  1958. udc->debugfs_usbd = usbd;
  1959. udc->debugfs_iudma = iudma;
  1960. return;
  1961. err_iudma:
  1962. debugfs_remove(usbd);
  1963. err_usbd:
  1964. debugfs_remove(root);
  1965. err_root:
  1966. dev_err(udc->dev, "debugfs is not available\n");
  1967. }
  1968. /**
  1969. * bcm63xx_udc_cleanup_debugfs - Remove debugfs entries.
  1970. * @udc: Reference to the device controller.
  1971. *
  1972. * debugfs_remove() is safe to call with a NULL argument.
  1973. */
  1974. static void bcm63xx_udc_cleanup_debugfs(struct bcm63xx_udc *udc)
  1975. {
  1976. debugfs_remove(udc->debugfs_iudma);
  1977. debugfs_remove(udc->debugfs_usbd);
  1978. debugfs_remove(udc->debugfs_root);
  1979. udc->debugfs_iudma = NULL;
  1980. udc->debugfs_usbd = NULL;
  1981. udc->debugfs_root = NULL;
  1982. }
  1983. /***********************************************************************
  1984. * Driver init/exit
  1985. ***********************************************************************/
  1986. /**
  1987. * bcm63xx_udc_gadget_release - Called from device_release().
  1988. * @dev: Unused.
  1989. *
  1990. * We get a warning if this function doesn't exist, but it's empty because
  1991. * we don't have to free any of the memory allocated with the devm_* APIs.
  1992. */
  1993. static void bcm63xx_udc_gadget_release(struct device *dev)
  1994. {
  1995. }
  1996. /**
  1997. * bcm63xx_udc_probe - Initialize a new instance of the UDC.
  1998. * @pdev: Platform device struct from the bcm63xx BSP code.
  1999. *
  2000. * Note that platform data is required, because pd.port_no varies from chip
  2001. * to chip and is used to switch the correct USB port to device mode.
  2002. */
  2003. static int bcm63xx_udc_probe(struct platform_device *pdev)
  2004. {
  2005. struct device *dev = &pdev->dev;
  2006. struct bcm63xx_usbd_platform_data *pd = dev->platform_data;
  2007. struct bcm63xx_udc *udc;
  2008. struct resource *res;
  2009. int rc = -ENOMEM, i, irq;
  2010. udc = devm_kzalloc(dev, sizeof(*udc), GFP_KERNEL);
  2011. if (!udc) {
  2012. dev_err(dev, "cannot allocate memory\n");
  2013. return -ENOMEM;
  2014. }
  2015. platform_set_drvdata(pdev, udc);
  2016. udc->dev = dev;
  2017. udc->pd = pd;
  2018. if (!pd) {
  2019. dev_err(dev, "missing platform data\n");
  2020. return -EINVAL;
  2021. }
  2022. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2023. if (!res) {
  2024. dev_err(dev, "error finding USBD resource\n");
  2025. return -ENXIO;
  2026. }
  2027. udc->usbd_regs = devm_ioremap_resource(dev, res);
  2028. if (IS_ERR(udc->usbd_regs))
  2029. return PTR_ERR(udc->usbd_regs);
  2030. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  2031. if (!res) {
  2032. dev_err(dev, "error finding IUDMA resource\n");
  2033. return -ENXIO;
  2034. }
  2035. udc->iudma_regs = devm_ioremap_resource(dev, res);
  2036. if (IS_ERR(udc->iudma_regs))
  2037. return PTR_ERR(udc->iudma_regs);
  2038. spin_lock_init(&udc->lock);
  2039. INIT_WORK(&udc->ep0_wq, bcm63xx_ep0_process);
  2040. dev_set_name(&udc->gadget.dev, "gadget");
  2041. udc->gadget.ops = &bcm63xx_udc_ops;
  2042. udc->gadget.name = dev_name(dev);
  2043. udc->gadget.dev.parent = dev;
  2044. udc->gadget.dev.release = bcm63xx_udc_gadget_release;
  2045. udc->gadget.dev.dma_mask = dev->dma_mask;
  2046. if (!pd->use_fullspeed && !use_fullspeed)
  2047. udc->gadget.max_speed = USB_SPEED_HIGH;
  2048. else
  2049. udc->gadget.max_speed = USB_SPEED_FULL;
  2050. /* request clocks, allocate buffers, and clear any pending IRQs */
  2051. rc = bcm63xx_init_udc_hw(udc);
  2052. if (rc)
  2053. return rc;
  2054. rc = -ENXIO;
  2055. /* IRQ resource #0: control interrupt (VBUS, speed, etc.) */
  2056. irq = platform_get_irq(pdev, 0);
  2057. if (irq < 0) {
  2058. dev_err(dev, "missing IRQ resource #0\n");
  2059. goto out_uninit;
  2060. }
  2061. if (devm_request_irq(dev, irq, &bcm63xx_udc_ctrl_isr, 0,
  2062. dev_name(dev), udc) < 0) {
  2063. dev_err(dev, "error requesting IRQ #%d\n", irq);
  2064. goto out_uninit;
  2065. }
  2066. /* IRQ resources #1-6: data interrupts for IUDMA channels 0-5 */
  2067. for (i = 0; i < BCM63XX_NUM_IUDMA; i++) {
  2068. irq = platform_get_irq(pdev, i + 1);
  2069. if (irq < 0) {
  2070. dev_err(dev, "missing IRQ resource #%d\n", i + 1);
  2071. goto out_uninit;
  2072. }
  2073. if (devm_request_irq(dev, irq, &bcm63xx_udc_data_isr, 0,
  2074. dev_name(dev), &udc->iudma[i]) < 0) {
  2075. dev_err(dev, "error requesting IRQ #%d\n", irq);
  2076. goto out_uninit;
  2077. }
  2078. }
  2079. rc = device_register(&udc->gadget.dev);
  2080. if (rc)
  2081. goto out_uninit;
  2082. bcm63xx_udc_init_debugfs(udc);
  2083. rc = usb_add_gadget_udc(dev, &udc->gadget);
  2084. if (!rc)
  2085. return 0;
  2086. bcm63xx_udc_cleanup_debugfs(udc);
  2087. device_unregister(&udc->gadget.dev);
  2088. out_uninit:
  2089. bcm63xx_uninit_udc_hw(udc);
  2090. return rc;
  2091. }
  2092. /**
  2093. * bcm63xx_udc_remove - Remove the device from the system.
  2094. * @pdev: Platform device struct from the bcm63xx BSP code.
  2095. */
  2096. static int bcm63xx_udc_remove(struct platform_device *pdev)
  2097. {
  2098. struct bcm63xx_udc *udc = platform_get_drvdata(pdev);
  2099. bcm63xx_udc_cleanup_debugfs(udc);
  2100. usb_del_gadget_udc(&udc->gadget);
  2101. device_unregister(&udc->gadget.dev);
  2102. BUG_ON(udc->driver);
  2103. platform_set_drvdata(pdev, NULL);
  2104. bcm63xx_uninit_udc_hw(udc);
  2105. return 0;
  2106. }
  2107. static struct platform_driver bcm63xx_udc_driver = {
  2108. .probe = bcm63xx_udc_probe,
  2109. .remove = bcm63xx_udc_remove,
  2110. .driver = {
  2111. .name = DRV_MODULE_NAME,
  2112. .owner = THIS_MODULE,
  2113. },
  2114. };
  2115. module_platform_driver(bcm63xx_udc_driver);
  2116. MODULE_DESCRIPTION("BCM63xx USB Peripheral Controller");
  2117. MODULE_AUTHOR("Kevin Cernekee <cernekee@gmail.com>");
  2118. MODULE_LICENSE("GPL");
  2119. MODULE_ALIAS("platform:" DRV_MODULE_NAME);