bcm63xx_udc.c 67 KB

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