renesas_usb3.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975
  1. /*
  2. * Renesas USB3.0 Peripheral driver (USB gadget)
  3. *
  4. * Copyright (C) 2015 Renesas Electronics Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/err.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/sizes.h>
  19. #include <linux/slab.h>
  20. #include <linux/usb/ch9.h>
  21. #include <linux/usb/gadget.h>
  22. /* register definitions */
  23. #define USB3_AXI_INT_STA 0x008
  24. #define USB3_AXI_INT_ENA 0x00c
  25. #define USB3_DMA_INT_STA 0x010
  26. #define USB3_DMA_INT_ENA 0x014
  27. #define USB3_USB_COM_CON 0x200
  28. #define USB3_USB20_CON 0x204
  29. #define USB3_USB30_CON 0x208
  30. #define USB3_USB_STA 0x210
  31. #define USB3_DRD_CON 0x218
  32. #define USB3_USB_INT_STA_1 0x220
  33. #define USB3_USB_INT_STA_2 0x224
  34. #define USB3_USB_INT_ENA_1 0x228
  35. #define USB3_USB_INT_ENA_2 0x22c
  36. #define USB3_STUP_DAT_0 0x230
  37. #define USB3_STUP_DAT_1 0x234
  38. #define USB3_P0_MOD 0x280
  39. #define USB3_P0_CON 0x288
  40. #define USB3_P0_STA 0x28c
  41. #define USB3_P0_INT_STA 0x290
  42. #define USB3_P0_INT_ENA 0x294
  43. #define USB3_P0_LNG 0x2a0
  44. #define USB3_P0_READ 0x2a4
  45. #define USB3_P0_WRITE 0x2a8
  46. #define USB3_PIPE_COM 0x2b0
  47. #define USB3_PN_MOD 0x2c0
  48. #define USB3_PN_RAMMAP 0x2c4
  49. #define USB3_PN_CON 0x2c8
  50. #define USB3_PN_STA 0x2cc
  51. #define USB3_PN_INT_STA 0x2d0
  52. #define USB3_PN_INT_ENA 0x2d4
  53. #define USB3_PN_LNG 0x2e0
  54. #define USB3_PN_READ 0x2e4
  55. #define USB3_PN_WRITE 0x2e8
  56. #define USB3_SSIFCMD 0x340
  57. /* AXI_INT_ENA and AXI_INT_STA */
  58. #define AXI_INT_DMAINT BIT(31)
  59. #define AXI_INT_EPCINT BIT(30)
  60. /* LCLKSEL */
  61. #define LCLKSEL_LSEL BIT(18)
  62. /* USB_COM_CON */
  63. #define USB_COM_CON_CONF BIT(24)
  64. #define USB_COM_CON_SPD_MODE BIT(17)
  65. #define USB_COM_CON_EP0_EN BIT(16)
  66. #define USB_COM_CON_DEV_ADDR_SHIFT 8
  67. #define USB_COM_CON_DEV_ADDR_MASK GENMASK(14, USB_COM_CON_DEV_ADDR_SHIFT)
  68. #define USB_COM_CON_DEV_ADDR(n) (((n) << USB_COM_CON_DEV_ADDR_SHIFT) & \
  69. USB_COM_CON_DEV_ADDR_MASK)
  70. #define USB_COM_CON_RX_DETECTION BIT(1)
  71. #define USB_COM_CON_PIPE_CLR BIT(0)
  72. /* USB20_CON */
  73. #define USB20_CON_B2_PUE BIT(31)
  74. #define USB20_CON_B2_SUSPEND BIT(24)
  75. #define USB20_CON_B2_CONNECT BIT(17)
  76. #define USB20_CON_B2_TSTMOD_SHIFT 8
  77. #define USB20_CON_B2_TSTMOD_MASK GENMASK(10, USB20_CON_B2_TSTMOD_SHIFT)
  78. #define USB20_CON_B2_TSTMOD(n) (((n) << USB20_CON_B2_TSTMOD_SHIFT) & \
  79. USB20_CON_B2_TSTMOD_MASK)
  80. #define USB20_CON_B2_TSTMOD_EN BIT(0)
  81. /* USB30_CON */
  82. #define USB30_CON_POW_SEL_SHIFT 24
  83. #define USB30_CON_POW_SEL_MASK GENMASK(26, USB30_CON_POW_SEL_SHIFT)
  84. #define USB30_CON_POW_SEL_IN_U3 BIT(26)
  85. #define USB30_CON_POW_SEL_IN_DISCON 0
  86. #define USB30_CON_POW_SEL_P2_TO_P0 BIT(25)
  87. #define USB30_CON_POW_SEL_P0_TO_P3 BIT(24)
  88. #define USB30_CON_POW_SEL_P0_TO_P2 0
  89. #define USB30_CON_B3_PLLWAKE BIT(23)
  90. #define USB30_CON_B3_CONNECT BIT(17)
  91. #define USB30_CON_B3_HOTRST_CMP BIT(1)
  92. /* USB_STA */
  93. #define USB_STA_SPEED_MASK (BIT(2) | BIT(1))
  94. #define USB_STA_SPEED_HS BIT(2)
  95. #define USB_STA_SPEED_FS BIT(1)
  96. #define USB_STA_SPEED_SS 0
  97. #define USB_STA_VBUS_STA BIT(0)
  98. /* DRD_CON */
  99. #define DRD_CON_PERI_CON BIT(24)
  100. /* USB_INT_ENA_1 and USB_INT_STA_1 */
  101. #define USB_INT_1_B3_PLLWKUP BIT(31)
  102. #define USB_INT_1_B3_LUPSUCS BIT(30)
  103. #define USB_INT_1_B3_DISABLE BIT(27)
  104. #define USB_INT_1_B3_WRMRST BIT(21)
  105. #define USB_INT_1_B3_HOTRST BIT(20)
  106. #define USB_INT_1_B2_USBRST BIT(12)
  107. #define USB_INT_1_B2_L1SPND BIT(11)
  108. #define USB_INT_1_B2_SPND BIT(9)
  109. #define USB_INT_1_B2_RSUM BIT(8)
  110. #define USB_INT_1_SPEED BIT(1)
  111. #define USB_INT_1_VBUS_CNG BIT(0)
  112. /* USB_INT_ENA_2 and USB_INT_STA_2 */
  113. #define USB_INT_2_PIPE(n) BIT(n)
  114. /* P0_MOD */
  115. #define P0_MOD_DIR BIT(6)
  116. /* P0_CON and PN_CON */
  117. #define PX_CON_BYTE_EN_MASK (BIT(10) | BIT(9))
  118. #define PX_CON_BYTE_EN_SHIFT 9
  119. #define PX_CON_BYTE_EN_BYTES(n) (((n) << PX_CON_BYTE_EN_SHIFT) & \
  120. PX_CON_BYTE_EN_MASK)
  121. #define PX_CON_SEND BIT(8)
  122. /* P0_CON */
  123. #define P0_CON_ST_RES_MASK (BIT(27) | BIT(26))
  124. #define P0_CON_ST_RES_FORCE_STALL BIT(27)
  125. #define P0_CON_ST_RES_NORMAL BIT(26)
  126. #define P0_CON_ST_RES_FORCE_NRDY 0
  127. #define P0_CON_OT_RES_MASK (BIT(25) | BIT(24))
  128. #define P0_CON_OT_RES_FORCE_STALL BIT(25)
  129. #define P0_CON_OT_RES_NORMAL BIT(24)
  130. #define P0_CON_OT_RES_FORCE_NRDY 0
  131. #define P0_CON_IN_RES_MASK (BIT(17) | BIT(16))
  132. #define P0_CON_IN_RES_FORCE_STALL BIT(17)
  133. #define P0_CON_IN_RES_NORMAL BIT(16)
  134. #define P0_CON_IN_RES_FORCE_NRDY 0
  135. #define P0_CON_RES_WEN BIT(7)
  136. #define P0_CON_BCLR BIT(1)
  137. /* P0_STA and PN_STA */
  138. #define PX_STA_BUFSTS BIT(0)
  139. /* P0_INT_ENA and P0_INT_STA */
  140. #define P0_INT_STSED BIT(18)
  141. #define P0_INT_STSST BIT(17)
  142. #define P0_INT_SETUP BIT(16)
  143. #define P0_INT_RCVNL BIT(8)
  144. #define P0_INT_ERDY BIT(7)
  145. #define P0_INT_FLOW BIT(6)
  146. #define P0_INT_STALL BIT(2)
  147. #define P0_INT_NRDY BIT(1)
  148. #define P0_INT_BFRDY BIT(0)
  149. #define P0_INT_ALL_BITS (P0_INT_STSED | P0_INT_SETUP | P0_INT_BFRDY)
  150. /* PN_MOD */
  151. #define PN_MOD_DIR BIT(6)
  152. #define PN_MOD_TYPE_SHIFT 4
  153. #define PN_MOD_TYPE_MASK GENMASK(5, PN_MOD_TYPE_SHIFT)
  154. #define PN_MOD_TYPE(n) (((n) << PN_MOD_TYPE_SHIFT) & \
  155. PN_MOD_TYPE_MASK)
  156. #define PN_MOD_EPNUM_MASK GENMASK(3, 0)
  157. #define PN_MOD_EPNUM(n) ((n) & PN_MOD_EPNUM_MASK)
  158. /* PN_RAMMAP */
  159. #define PN_RAMMAP_RAMAREA_SHIFT 29
  160. #define PN_RAMMAP_RAMAREA_MASK GENMASK(31, PN_RAMMAP_RAMAREA_SHIFT)
  161. #define PN_RAMMAP_RAMAREA_16KB BIT(31)
  162. #define PN_RAMMAP_RAMAREA_8KB (BIT(30) | BIT(29))
  163. #define PN_RAMMAP_RAMAREA_4KB BIT(30)
  164. #define PN_RAMMAP_RAMAREA_2KB BIT(29)
  165. #define PN_RAMMAP_RAMAREA_1KB 0
  166. #define PN_RAMMAP_MPKT_SHIFT 16
  167. #define PN_RAMMAP_MPKT_MASK GENMASK(26, PN_RAMMAP_MPKT_SHIFT)
  168. #define PN_RAMMAP_MPKT(n) (((n) << PN_RAMMAP_MPKT_SHIFT) & \
  169. PN_RAMMAP_MPKT_MASK)
  170. #define PN_RAMMAP_RAMIF_SHIFT 14
  171. #define PN_RAMMAP_RAMIF_MASK GENMASK(15, PN_RAMMAP_RAMIF_SHIFT)
  172. #define PN_RAMMAP_RAMIF(n) (((n) << PN_RAMMAP_RAMIF_SHIFT) & \
  173. PN_RAMMAP_RAMIF_MASK)
  174. #define PN_RAMMAP_BASEAD_MASK GENMASK(13, 0)
  175. #define PN_RAMMAP_BASEAD(offs) (((offs) >> 3) & PN_RAMMAP_BASEAD_MASK)
  176. #define PN_RAMMAP_DATA(area, ramif, basead) ((PN_RAMMAP_##area) | \
  177. (PN_RAMMAP_RAMIF(ramif)) | \
  178. (PN_RAMMAP_BASEAD(basead)))
  179. /* PN_CON */
  180. #define PN_CON_EN BIT(31)
  181. #define PN_CON_DATAIF_EN BIT(30)
  182. #define PN_CON_RES_MASK (BIT(17) | BIT(16))
  183. #define PN_CON_RES_FORCE_STALL BIT(17)
  184. #define PN_CON_RES_NORMAL BIT(16)
  185. #define PN_CON_RES_FORCE_NRDY 0
  186. #define PN_CON_LAST BIT(11)
  187. #define PN_CON_RES_WEN BIT(7)
  188. #define PN_CON_CLR BIT(0)
  189. /* PN_INT_STA and PN_INT_ENA */
  190. #define PN_INT_LSTTR BIT(4)
  191. #define PN_INT_BFRDY BIT(0)
  192. /* USB3_SSIFCMD */
  193. #define SSIFCMD_URES_U2 BIT(9)
  194. #define SSIFCMD_URES_U1 BIT(8)
  195. #define SSIFCMD_UDIR_U2 BIT(7)
  196. #define SSIFCMD_UDIR_U1 BIT(6)
  197. #define SSIFCMD_UREQ_U2 BIT(5)
  198. #define SSIFCMD_UREQ_U1 BIT(4)
  199. #define USB3_EP0_SS_MAX_PACKET_SIZE 512
  200. #define USB3_EP0_HSFS_MAX_PACKET_SIZE 64
  201. #define USB3_EP0_BUF_SIZE 8
  202. #define USB3_MAX_NUM_PIPES 30
  203. #define USB3_WAIT_US 3
  204. struct renesas_usb3;
  205. struct renesas_usb3_request {
  206. struct usb_request req;
  207. struct list_head queue;
  208. };
  209. #define USB3_EP_NAME_SIZE 8
  210. struct renesas_usb3_ep {
  211. struct usb_ep ep;
  212. struct renesas_usb3 *usb3;
  213. int num;
  214. char ep_name[USB3_EP_NAME_SIZE];
  215. struct list_head queue;
  216. u32 rammap_val;
  217. bool dir_in;
  218. bool halt;
  219. bool wedge;
  220. bool started;
  221. };
  222. struct renesas_usb3_priv {
  223. int ramsize_per_ramif; /* unit = bytes */
  224. int num_ramif;
  225. int ramsize_per_pipe; /* unit = bytes */
  226. bool workaround_for_vbus; /* if true, don't check vbus signal */
  227. };
  228. struct renesas_usb3 {
  229. void __iomem *reg;
  230. struct usb_gadget gadget;
  231. struct usb_gadget_driver *driver;
  232. struct renesas_usb3_ep *usb3_ep;
  233. int num_usb3_eps;
  234. spinlock_t lock;
  235. int disabled_count;
  236. struct usb_request *ep0_req;
  237. u16 test_mode;
  238. u8 ep0_buf[USB3_EP0_BUF_SIZE];
  239. bool softconnect;
  240. bool workaround_for_vbus;
  241. };
  242. #define gadget_to_renesas_usb3(_gadget) \
  243. container_of(_gadget, struct renesas_usb3, gadget)
  244. #define renesas_usb3_to_gadget(renesas_usb3) (&renesas_usb3->gadget)
  245. #define usb3_to_dev(_usb3) (_usb3->gadget.dev.parent)
  246. #define usb_ep_to_usb3_ep(_ep) container_of(_ep, struct renesas_usb3_ep, ep)
  247. #define usb3_ep_to_usb3(_usb3_ep) (_usb3_ep->usb3)
  248. #define usb_req_to_usb3_req(_req) container_of(_req, \
  249. struct renesas_usb3_request, req)
  250. #define usb3_get_ep(usb3, n) ((usb3)->usb3_ep + (n))
  251. #define usb3_for_each_ep(usb3_ep, usb3, i) \
  252. for ((i) = 0, usb3_ep = usb3_get_ep(usb3, (i)); \
  253. (i) < (usb3)->num_usb3_eps; \
  254. (i)++, usb3_ep = usb3_get_ep(usb3, (i)))
  255. static const char udc_name[] = "renesas_usb3";
  256. static void usb3_write(struct renesas_usb3 *usb3, u32 data, u32 offs)
  257. {
  258. iowrite32(data, usb3->reg + offs);
  259. }
  260. static u32 usb3_read(struct renesas_usb3 *usb3, u32 offs)
  261. {
  262. return ioread32(usb3->reg + offs);
  263. }
  264. static void usb3_set_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs)
  265. {
  266. u32 val = usb3_read(usb3, offs);
  267. val |= bits;
  268. usb3_write(usb3, val, offs);
  269. }
  270. static void usb3_clear_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs)
  271. {
  272. u32 val = usb3_read(usb3, offs);
  273. val &= ~bits;
  274. usb3_write(usb3, val, offs);
  275. }
  276. static int usb3_wait(struct renesas_usb3 *usb3, u32 reg, u32 mask,
  277. u32 expected)
  278. {
  279. int i;
  280. for (i = 0; i < USB3_WAIT_US; i++) {
  281. if ((usb3_read(usb3, reg) & mask) == expected)
  282. return 0;
  283. udelay(1);
  284. }
  285. dev_dbg(usb3_to_dev(usb3), "%s: timed out (%8x, %08x, %08x)\n",
  286. __func__, reg, mask, expected);
  287. return -EBUSY;
  288. }
  289. static void usb3_enable_irq_1(struct renesas_usb3 *usb3, u32 bits)
  290. {
  291. usb3_set_bit(usb3, bits, USB3_USB_INT_ENA_1);
  292. }
  293. static void usb3_disable_irq_1(struct renesas_usb3 *usb3, u32 bits)
  294. {
  295. usb3_clear_bit(usb3, bits, USB3_USB_INT_ENA_1);
  296. }
  297. static void usb3_enable_pipe_irq(struct renesas_usb3 *usb3, int num)
  298. {
  299. usb3_set_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2);
  300. }
  301. static void usb3_disable_pipe_irq(struct renesas_usb3 *usb3, int num)
  302. {
  303. usb3_clear_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2);
  304. }
  305. static void usb3_init_axi_bridge(struct renesas_usb3 *usb3)
  306. {
  307. /* Set AXI_INT */
  308. usb3_write(usb3, ~0, USB3_DMA_INT_STA);
  309. usb3_write(usb3, 0, USB3_DMA_INT_ENA);
  310. usb3_set_bit(usb3, AXI_INT_DMAINT | AXI_INT_EPCINT, USB3_AXI_INT_ENA);
  311. }
  312. static void usb3_init_epc_registers(struct renesas_usb3 *usb3)
  313. {
  314. /* FIXME: How to change host / peripheral mode as well? */
  315. usb3_set_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON);
  316. usb3_write(usb3, ~0, USB3_USB_INT_STA_1);
  317. usb3_enable_irq_1(usb3, USB_INT_1_VBUS_CNG);
  318. }
  319. static bool usb3_wakeup_usb2_phy(struct renesas_usb3 *usb3)
  320. {
  321. if (!(usb3_read(usb3, USB3_USB20_CON) & USB20_CON_B2_SUSPEND))
  322. return true; /* already waked it up */
  323. usb3_clear_bit(usb3, USB20_CON_B2_SUSPEND, USB3_USB20_CON);
  324. usb3_enable_irq_1(usb3, USB_INT_1_B2_RSUM);
  325. return false;
  326. }
  327. static void usb3_usb2_pullup(struct renesas_usb3 *usb3, int pullup)
  328. {
  329. u32 bits = USB20_CON_B2_PUE | USB20_CON_B2_CONNECT;
  330. if (usb3->softconnect && pullup)
  331. usb3_set_bit(usb3, bits, USB3_USB20_CON);
  332. else
  333. usb3_clear_bit(usb3, bits, USB3_USB20_CON);
  334. }
  335. static void usb3_set_test_mode(struct renesas_usb3 *usb3)
  336. {
  337. u32 val = usb3_read(usb3, USB3_USB20_CON);
  338. val &= ~USB20_CON_B2_TSTMOD_MASK;
  339. val |= USB20_CON_B2_TSTMOD(usb3->test_mode);
  340. usb3_write(usb3, val | USB20_CON_B2_TSTMOD_EN, USB3_USB20_CON);
  341. if (!usb3->test_mode)
  342. usb3_clear_bit(usb3, USB20_CON_B2_TSTMOD_EN, USB3_USB20_CON);
  343. }
  344. static void usb3_start_usb2_connection(struct renesas_usb3 *usb3)
  345. {
  346. usb3->disabled_count++;
  347. usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
  348. usb3_set_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON);
  349. usb3_usb2_pullup(usb3, 1);
  350. }
  351. static int usb3_is_usb3_phy_in_u3(struct renesas_usb3 *usb3)
  352. {
  353. return usb3_read(usb3, USB3_USB30_CON) & USB30_CON_POW_SEL_IN_U3;
  354. }
  355. static bool usb3_wakeup_usb3_phy(struct renesas_usb3 *usb3)
  356. {
  357. if (!usb3_is_usb3_phy_in_u3(usb3))
  358. return true; /* already waked it up */
  359. usb3_set_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON);
  360. usb3_enable_irq_1(usb3, USB_INT_1_B3_PLLWKUP);
  361. return false;
  362. }
  363. static u16 usb3_feature_get_un_enabled(struct renesas_usb3 *usb3)
  364. {
  365. u32 mask_u2 = SSIFCMD_UDIR_U2 | SSIFCMD_UREQ_U2;
  366. u32 mask_u1 = SSIFCMD_UDIR_U1 | SSIFCMD_UREQ_U1;
  367. u32 val = usb3_read(usb3, USB3_SSIFCMD);
  368. u16 ret = 0;
  369. /* Enables {U2,U1} if the bits of UDIR and UREQ are set to 0 */
  370. if (!(val & mask_u2))
  371. ret |= 1 << USB_DEV_STAT_U2_ENABLED;
  372. if (!(val & mask_u1))
  373. ret |= 1 << USB_DEV_STAT_U1_ENABLED;
  374. return ret;
  375. }
  376. static void usb3_feature_u2_enable(struct renesas_usb3 *usb3, bool enable)
  377. {
  378. u32 bits = SSIFCMD_UDIR_U2 | SSIFCMD_UREQ_U2;
  379. /* Enables U2 if the bits of UDIR and UREQ are set to 0 */
  380. if (enable)
  381. usb3_clear_bit(usb3, bits, USB3_SSIFCMD);
  382. else
  383. usb3_set_bit(usb3, bits, USB3_SSIFCMD);
  384. }
  385. static void usb3_feature_u1_enable(struct renesas_usb3 *usb3, bool enable)
  386. {
  387. u32 bits = SSIFCMD_UDIR_U1 | SSIFCMD_UREQ_U1;
  388. /* Enables U1 if the bits of UDIR and UREQ are set to 0 */
  389. if (enable)
  390. usb3_clear_bit(usb3, bits, USB3_SSIFCMD);
  391. else
  392. usb3_set_bit(usb3, bits, USB3_SSIFCMD);
  393. }
  394. static void usb3_start_operation_for_usb3(struct renesas_usb3 *usb3)
  395. {
  396. usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
  397. usb3_clear_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON);
  398. usb3_set_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
  399. }
  400. static void usb3_start_usb3_connection(struct renesas_usb3 *usb3)
  401. {
  402. usb3_start_operation_for_usb3(usb3);
  403. usb3_set_bit(usb3, USB_COM_CON_RX_DETECTION, USB3_USB_COM_CON);
  404. usb3_enable_irq_1(usb3, USB_INT_1_B3_LUPSUCS | USB_INT_1_B3_DISABLE |
  405. USB_INT_1_SPEED);
  406. }
  407. static void usb3_stop_usb3_connection(struct renesas_usb3 *usb3)
  408. {
  409. usb3_clear_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
  410. }
  411. static void usb3_transition_to_default_state(struct renesas_usb3 *usb3,
  412. bool is_usb3)
  413. {
  414. usb3_set_bit(usb3, USB_INT_2_PIPE(0), USB3_USB_INT_ENA_2);
  415. usb3_write(usb3, P0_INT_ALL_BITS, USB3_P0_INT_STA);
  416. usb3_set_bit(usb3, P0_INT_ALL_BITS, USB3_P0_INT_ENA);
  417. if (is_usb3)
  418. usb3_enable_irq_1(usb3, USB_INT_1_B3_WRMRST |
  419. USB_INT_1_B3_HOTRST);
  420. else
  421. usb3_enable_irq_1(usb3, USB_INT_1_B2_SPND |
  422. USB_INT_1_B2_L1SPND | USB_INT_1_B2_USBRST);
  423. }
  424. static void usb3_connect(struct renesas_usb3 *usb3)
  425. {
  426. if (usb3_wakeup_usb3_phy(usb3))
  427. usb3_start_usb3_connection(usb3);
  428. }
  429. static void usb3_reset_epc(struct renesas_usb3 *usb3)
  430. {
  431. usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
  432. usb3_clear_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
  433. usb3_set_bit(usb3, USB_COM_CON_PIPE_CLR, USB3_USB_COM_CON);
  434. usb3->test_mode = 0;
  435. usb3_set_test_mode(usb3);
  436. }
  437. static void usb3_disconnect(struct renesas_usb3 *usb3)
  438. {
  439. usb3->disabled_count = 0;
  440. usb3_usb2_pullup(usb3, 0);
  441. usb3_clear_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON);
  442. usb3_reset_epc(usb3);
  443. if (usb3->driver)
  444. usb3->driver->disconnect(&usb3->gadget);
  445. }
  446. static void usb3_check_vbus(struct renesas_usb3 *usb3)
  447. {
  448. if (usb3->workaround_for_vbus) {
  449. usb3_connect(usb3);
  450. } else {
  451. if (usb3_read(usb3, USB3_USB_STA) & USB_STA_VBUS_STA)
  452. usb3_connect(usb3);
  453. else
  454. usb3_disconnect(usb3);
  455. }
  456. }
  457. static void renesas_usb3_init_controller(struct renesas_usb3 *usb3)
  458. {
  459. usb3_init_axi_bridge(usb3);
  460. usb3_init_epc_registers(usb3);
  461. usb3_check_vbus(usb3);
  462. }
  463. static void renesas_usb3_stop_controller(struct renesas_usb3 *usb3)
  464. {
  465. usb3_disconnect(usb3);
  466. usb3_write(usb3, 0, USB3_P0_INT_ENA);
  467. usb3_write(usb3, 0, USB3_PN_INT_ENA);
  468. usb3_write(usb3, 0, USB3_USB_INT_ENA_1);
  469. usb3_write(usb3, 0, USB3_USB_INT_ENA_2);
  470. usb3_write(usb3, 0, USB3_AXI_INT_ENA);
  471. }
  472. static void usb3_irq_epc_int_1_pll_wakeup(struct renesas_usb3 *usb3)
  473. {
  474. usb3_disable_irq_1(usb3, USB_INT_1_B3_PLLWKUP);
  475. usb3_clear_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON);
  476. usb3_start_usb3_connection(usb3);
  477. }
  478. static void usb3_irq_epc_int_1_linkup_success(struct renesas_usb3 *usb3)
  479. {
  480. usb3_transition_to_default_state(usb3, true);
  481. }
  482. static void usb3_irq_epc_int_1_resume(struct renesas_usb3 *usb3)
  483. {
  484. usb3_disable_irq_1(usb3, USB_INT_1_B2_RSUM);
  485. usb3_start_usb2_connection(usb3);
  486. usb3_transition_to_default_state(usb3, false);
  487. }
  488. static void usb3_irq_epc_int_1_disable(struct renesas_usb3 *usb3)
  489. {
  490. usb3_stop_usb3_connection(usb3);
  491. if (usb3_wakeup_usb2_phy(usb3))
  492. usb3_irq_epc_int_1_resume(usb3);
  493. }
  494. static void usb3_irq_epc_int_1_bus_reset(struct renesas_usb3 *usb3)
  495. {
  496. usb3_reset_epc(usb3);
  497. if (usb3->disabled_count < 3)
  498. usb3_start_usb3_connection(usb3);
  499. else
  500. usb3_start_usb2_connection(usb3);
  501. }
  502. static void usb3_irq_epc_int_1_vbus_change(struct renesas_usb3 *usb3)
  503. {
  504. usb3_check_vbus(usb3);
  505. }
  506. static void usb3_irq_epc_int_1_hot_reset(struct renesas_usb3 *usb3)
  507. {
  508. usb3_reset_epc(usb3);
  509. usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
  510. /* This bit shall be set within 12ms from the start of HotReset */
  511. usb3_set_bit(usb3, USB30_CON_B3_HOTRST_CMP, USB3_USB30_CON);
  512. }
  513. static void usb3_irq_epc_int_1_warm_reset(struct renesas_usb3 *usb3)
  514. {
  515. usb3_reset_epc(usb3);
  516. usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON);
  517. usb3_start_operation_for_usb3(usb3);
  518. usb3_enable_irq_1(usb3, USB_INT_1_SPEED);
  519. }
  520. static void usb3_irq_epc_int_1_speed(struct renesas_usb3 *usb3)
  521. {
  522. u32 speed = usb3_read(usb3, USB3_USB_STA) & USB_STA_SPEED_MASK;
  523. switch (speed) {
  524. case USB_STA_SPEED_SS:
  525. usb3->gadget.speed = USB_SPEED_SUPER;
  526. break;
  527. case USB_STA_SPEED_HS:
  528. usb3->gadget.speed = USB_SPEED_HIGH;
  529. break;
  530. case USB_STA_SPEED_FS:
  531. usb3->gadget.speed = USB_SPEED_FULL;
  532. break;
  533. default:
  534. usb3->gadget.speed = USB_SPEED_UNKNOWN;
  535. break;
  536. }
  537. }
  538. static void usb3_irq_epc_int_1(struct renesas_usb3 *usb3, u32 int_sta_1)
  539. {
  540. if (int_sta_1 & USB_INT_1_B3_PLLWKUP)
  541. usb3_irq_epc_int_1_pll_wakeup(usb3);
  542. if (int_sta_1 & USB_INT_1_B3_LUPSUCS)
  543. usb3_irq_epc_int_1_linkup_success(usb3);
  544. if (int_sta_1 & USB_INT_1_B3_HOTRST)
  545. usb3_irq_epc_int_1_hot_reset(usb3);
  546. if (int_sta_1 & USB_INT_1_B3_WRMRST)
  547. usb3_irq_epc_int_1_warm_reset(usb3);
  548. if (int_sta_1 & USB_INT_1_B3_DISABLE)
  549. usb3_irq_epc_int_1_disable(usb3);
  550. if (int_sta_1 & USB_INT_1_B2_USBRST)
  551. usb3_irq_epc_int_1_bus_reset(usb3);
  552. if (int_sta_1 & USB_INT_1_B2_RSUM)
  553. usb3_irq_epc_int_1_resume(usb3);
  554. if (int_sta_1 & USB_INT_1_SPEED)
  555. usb3_irq_epc_int_1_speed(usb3);
  556. if (int_sta_1 & USB_INT_1_VBUS_CNG)
  557. usb3_irq_epc_int_1_vbus_change(usb3);
  558. }
  559. static struct renesas_usb3_request *__usb3_get_request(struct renesas_usb3_ep
  560. *usb3_ep)
  561. {
  562. return list_first_entry_or_null(&usb3_ep->queue,
  563. struct renesas_usb3_request, queue);
  564. }
  565. static struct renesas_usb3_request *usb3_get_request(struct renesas_usb3_ep
  566. *usb3_ep)
  567. {
  568. struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
  569. struct renesas_usb3_request *usb3_req;
  570. unsigned long flags;
  571. spin_lock_irqsave(&usb3->lock, flags);
  572. usb3_req = __usb3_get_request(usb3_ep);
  573. spin_unlock_irqrestore(&usb3->lock, flags);
  574. return usb3_req;
  575. }
  576. static void usb3_request_done(struct renesas_usb3_ep *usb3_ep,
  577. struct renesas_usb3_request *usb3_req, int status)
  578. {
  579. struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
  580. unsigned long flags;
  581. dev_dbg(usb3_to_dev(usb3), "giveback: ep%2d, %u, %u, %d\n",
  582. usb3_ep->num, usb3_req->req.length, usb3_req->req.actual,
  583. status);
  584. usb3_req->req.status = status;
  585. spin_lock_irqsave(&usb3->lock, flags);
  586. usb3_ep->started = false;
  587. list_del_init(&usb3_req->queue);
  588. spin_unlock_irqrestore(&usb3->lock, flags);
  589. usb_gadget_giveback_request(&usb3_ep->ep, &usb3_req->req);
  590. }
  591. static void usb3_irq_epc_pipe0_status_end(struct renesas_usb3 *usb3)
  592. {
  593. struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
  594. struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
  595. if (usb3_req)
  596. usb3_request_done(usb3_ep, usb3_req, 0);
  597. if (usb3->test_mode)
  598. usb3_set_test_mode(usb3);
  599. }
  600. static void usb3_get_setup_data(struct renesas_usb3 *usb3,
  601. struct usb_ctrlrequest *ctrl)
  602. {
  603. struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
  604. u32 *data = (u32 *)ctrl;
  605. *data++ = usb3_read(usb3, USB3_STUP_DAT_0);
  606. *data = usb3_read(usb3, USB3_STUP_DAT_1);
  607. /* update this driver's flag */
  608. usb3_ep->dir_in = !!(ctrl->bRequestType & USB_DIR_IN);
  609. }
  610. static void usb3_set_p0_con_update_res(struct renesas_usb3 *usb3, u32 res)
  611. {
  612. u32 val = usb3_read(usb3, USB3_P0_CON);
  613. val &= ~(P0_CON_ST_RES_MASK | P0_CON_OT_RES_MASK | P0_CON_IN_RES_MASK);
  614. val |= res | P0_CON_RES_WEN;
  615. usb3_write(usb3, val, USB3_P0_CON);
  616. }
  617. static void usb3_set_p0_con_for_ctrl_read_data(struct renesas_usb3 *usb3)
  618. {
  619. usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
  620. P0_CON_OT_RES_FORCE_STALL |
  621. P0_CON_IN_RES_NORMAL);
  622. }
  623. static void usb3_set_p0_con_for_ctrl_read_status(struct renesas_usb3 *usb3)
  624. {
  625. usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
  626. P0_CON_OT_RES_FORCE_STALL |
  627. P0_CON_IN_RES_NORMAL);
  628. }
  629. static void usb3_set_p0_con_for_ctrl_write_data(struct renesas_usb3 *usb3)
  630. {
  631. usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
  632. P0_CON_OT_RES_NORMAL |
  633. P0_CON_IN_RES_FORCE_STALL);
  634. }
  635. static void usb3_set_p0_con_for_ctrl_write_status(struct renesas_usb3 *usb3)
  636. {
  637. usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
  638. P0_CON_OT_RES_NORMAL |
  639. P0_CON_IN_RES_FORCE_STALL);
  640. }
  641. static void usb3_set_p0_con_for_no_data(struct renesas_usb3 *usb3)
  642. {
  643. usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL |
  644. P0_CON_OT_RES_FORCE_STALL |
  645. P0_CON_IN_RES_FORCE_STALL);
  646. }
  647. static void usb3_set_p0_con_stall(struct renesas_usb3 *usb3)
  648. {
  649. usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_STALL |
  650. P0_CON_OT_RES_FORCE_STALL |
  651. P0_CON_IN_RES_FORCE_STALL);
  652. }
  653. static void usb3_set_p0_con_stop(struct renesas_usb3 *usb3)
  654. {
  655. usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY |
  656. P0_CON_OT_RES_FORCE_NRDY |
  657. P0_CON_IN_RES_FORCE_NRDY);
  658. }
  659. static int usb3_pn_change(struct renesas_usb3 *usb3, int num)
  660. {
  661. if (num == 0 || num > usb3->num_usb3_eps)
  662. return -ENXIO;
  663. usb3_write(usb3, num, USB3_PIPE_COM);
  664. return 0;
  665. }
  666. static void usb3_set_pn_con_update_res(struct renesas_usb3 *usb3, u32 res)
  667. {
  668. u32 val = usb3_read(usb3, USB3_PN_CON);
  669. val &= ~PN_CON_RES_MASK;
  670. val |= res & PN_CON_RES_MASK;
  671. val |= PN_CON_RES_WEN;
  672. usb3_write(usb3, val, USB3_PN_CON);
  673. }
  674. static void usb3_pn_start(struct renesas_usb3 *usb3)
  675. {
  676. usb3_set_pn_con_update_res(usb3, PN_CON_RES_NORMAL);
  677. }
  678. static void usb3_pn_stop(struct renesas_usb3 *usb3)
  679. {
  680. usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_NRDY);
  681. }
  682. static void usb3_pn_stall(struct renesas_usb3 *usb3)
  683. {
  684. usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_STALL);
  685. }
  686. static int usb3_pn_con_clear(struct renesas_usb3 *usb3)
  687. {
  688. usb3_set_bit(usb3, PN_CON_CLR, USB3_PN_CON);
  689. return usb3_wait(usb3, USB3_PN_CON, PN_CON_CLR, 0);
  690. }
  691. static bool usb3_is_transfer_complete(struct renesas_usb3_ep *usb3_ep,
  692. struct renesas_usb3_request *usb3_req)
  693. {
  694. struct usb_request *req = &usb3_req->req;
  695. if ((!req->zero && req->actual == req->length) ||
  696. (req->actual % usb3_ep->ep.maxpacket) || (req->length == 0))
  697. return true;
  698. else
  699. return false;
  700. }
  701. static int usb3_wait_pipe_status(struct renesas_usb3_ep *usb3_ep, u32 mask)
  702. {
  703. struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
  704. u32 sta_reg = usb3_ep->num ? USB3_PN_STA : USB3_P0_STA;
  705. return usb3_wait(usb3, sta_reg, mask, mask);
  706. }
  707. static void usb3_set_px_con_send(struct renesas_usb3_ep *usb3_ep, int bytes,
  708. bool last)
  709. {
  710. struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
  711. u32 con_reg = usb3_ep->num ? USB3_PN_CON : USB3_P0_CON;
  712. u32 val = usb3_read(usb3, con_reg);
  713. val |= PX_CON_SEND | PX_CON_BYTE_EN_BYTES(bytes);
  714. val |= (usb3_ep->num && last) ? PN_CON_LAST : 0;
  715. usb3_write(usb3, val, con_reg);
  716. }
  717. static int usb3_write_pipe(struct renesas_usb3_ep *usb3_ep,
  718. struct renesas_usb3_request *usb3_req,
  719. u32 fifo_reg)
  720. {
  721. struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
  722. int i;
  723. int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual,
  724. usb3_ep->ep.maxpacket);
  725. u8 *buf = usb3_req->req.buf + usb3_req->req.actual;
  726. u32 tmp = 0;
  727. bool is_last;
  728. if (usb3_wait_pipe_status(usb3_ep, PX_STA_BUFSTS) < 0)
  729. return -EBUSY;
  730. /* Update gadget driver parameter */
  731. usb3_req->req.actual += len;
  732. /* Write data to the register */
  733. if (len >= 4) {
  734. iowrite32_rep(usb3->reg + fifo_reg, buf, len / 4);
  735. buf += (len / 4) * 4;
  736. len %= 4; /* update len to use usb3_set_pX_con_send() */
  737. }
  738. if (len) {
  739. for (i = 0; i < len; i++)
  740. tmp |= buf[i] << (8 * i);
  741. usb3_write(usb3, tmp, fifo_reg);
  742. }
  743. is_last = usb3_is_transfer_complete(usb3_ep, usb3_req);
  744. /* Send the data */
  745. usb3_set_px_con_send(usb3_ep, len, is_last);
  746. return is_last ? 0 : -EAGAIN;
  747. }
  748. static u32 usb3_get_received_length(struct renesas_usb3_ep *usb3_ep)
  749. {
  750. struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
  751. u32 lng_reg = usb3_ep->num ? USB3_PN_LNG : USB3_P0_LNG;
  752. return usb3_read(usb3, lng_reg);
  753. }
  754. static int usb3_read_pipe(struct renesas_usb3_ep *usb3_ep,
  755. struct renesas_usb3_request *usb3_req, u32 fifo_reg)
  756. {
  757. struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
  758. int i;
  759. int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual,
  760. usb3_get_received_length(usb3_ep));
  761. u8 *buf = usb3_req->req.buf + usb3_req->req.actual;
  762. u32 tmp = 0;
  763. if (!len)
  764. return 0;
  765. /* Update gadget driver parameter */
  766. usb3_req->req.actual += len;
  767. /* Read data from the register */
  768. if (len >= 4) {
  769. ioread32_rep(usb3->reg + fifo_reg, buf, len / 4);
  770. buf += (len / 4) * 4;
  771. len %= 4;
  772. }
  773. if (len) {
  774. tmp = usb3_read(usb3, fifo_reg);
  775. for (i = 0; i < len; i++)
  776. buf[i] = (tmp >> (8 * i)) & 0xff;
  777. }
  778. return usb3_is_transfer_complete(usb3_ep, usb3_req) ? 0 : -EAGAIN;
  779. }
  780. static void usb3_set_status_stage(struct renesas_usb3_ep *usb3_ep,
  781. struct renesas_usb3_request *usb3_req)
  782. {
  783. struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
  784. if (usb3_ep->dir_in) {
  785. usb3_set_p0_con_for_ctrl_read_status(usb3);
  786. } else {
  787. if (!usb3_req->req.length)
  788. usb3_set_p0_con_for_no_data(usb3);
  789. else
  790. usb3_set_p0_con_for_ctrl_write_status(usb3);
  791. }
  792. }
  793. static void usb3_p0_xfer(struct renesas_usb3_ep *usb3_ep,
  794. struct renesas_usb3_request *usb3_req)
  795. {
  796. int ret = -EAGAIN;
  797. if (usb3_ep->dir_in)
  798. ret = usb3_write_pipe(usb3_ep, usb3_req, USB3_P0_WRITE);
  799. else
  800. ret = usb3_read_pipe(usb3_ep, usb3_req, USB3_P0_READ);
  801. if (!ret)
  802. usb3_set_status_stage(usb3_ep, usb3_req);
  803. }
  804. static void usb3_start_pipe0(struct renesas_usb3_ep *usb3_ep,
  805. struct renesas_usb3_request *usb3_req)
  806. {
  807. struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
  808. if (usb3_ep->started)
  809. return;
  810. usb3_ep->started = true;
  811. if (usb3_ep->dir_in) {
  812. usb3_set_bit(usb3, P0_MOD_DIR, USB3_P0_MOD);
  813. usb3_set_p0_con_for_ctrl_read_data(usb3);
  814. } else {
  815. usb3_clear_bit(usb3, P0_MOD_DIR, USB3_P0_MOD);
  816. usb3_set_p0_con_for_ctrl_write_data(usb3);
  817. }
  818. usb3_p0_xfer(usb3_ep, usb3_req);
  819. }
  820. static void usb3_start_pipen(struct renesas_usb3_ep *usb3_ep,
  821. struct renesas_usb3_request *usb3_req)
  822. {
  823. struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
  824. struct renesas_usb3_request *usb3_req_first = usb3_get_request(usb3_ep);
  825. unsigned long flags;
  826. int ret = -EAGAIN;
  827. u32 enable_bits = 0;
  828. if (usb3_ep->halt || usb3_ep->started)
  829. return;
  830. if (usb3_req != usb3_req_first)
  831. return;
  832. spin_lock_irqsave(&usb3->lock, flags);
  833. if (usb3_pn_change(usb3, usb3_ep->num) < 0)
  834. goto out;
  835. usb3_ep->started = true;
  836. usb3_pn_start(usb3);
  837. if (usb3_ep->dir_in) {
  838. ret = usb3_write_pipe(usb3_ep, usb3_req, USB3_PN_WRITE);
  839. enable_bits |= PN_INT_LSTTR;
  840. }
  841. if (ret < 0)
  842. enable_bits |= PN_INT_BFRDY;
  843. if (enable_bits) {
  844. usb3_set_bit(usb3, enable_bits, USB3_PN_INT_ENA);
  845. usb3_enable_pipe_irq(usb3, usb3_ep->num);
  846. }
  847. out:
  848. spin_unlock_irqrestore(&usb3->lock, flags);
  849. }
  850. static int renesas_usb3_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
  851. gfp_t gfp_flags)
  852. {
  853. struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
  854. struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
  855. struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
  856. unsigned long flags;
  857. dev_dbg(usb3_to_dev(usb3), "ep_queue: ep%2d, %u\n", usb3_ep->num,
  858. _req->length);
  859. _req->status = -EINPROGRESS;
  860. _req->actual = 0;
  861. spin_lock_irqsave(&usb3->lock, flags);
  862. list_add_tail(&usb3_req->queue, &usb3_ep->queue);
  863. spin_unlock_irqrestore(&usb3->lock, flags);
  864. if (!usb3_ep->num)
  865. usb3_start_pipe0(usb3_ep, usb3_req);
  866. else
  867. usb3_start_pipen(usb3_ep, usb3_req);
  868. return 0;
  869. }
  870. static void usb3_set_device_address(struct renesas_usb3 *usb3, u16 addr)
  871. {
  872. /* DEV_ADDR bit field is cleared by WarmReset, HotReset and BusReset */
  873. usb3_set_bit(usb3, USB_COM_CON_DEV_ADDR(addr), USB3_USB_COM_CON);
  874. }
  875. static bool usb3_std_req_set_address(struct renesas_usb3 *usb3,
  876. struct usb_ctrlrequest *ctrl)
  877. {
  878. if (ctrl->wValue >= 128)
  879. return true; /* stall */
  880. usb3_set_device_address(usb3, ctrl->wValue);
  881. usb3_set_p0_con_for_no_data(usb3);
  882. return false;
  883. }
  884. static void usb3_pipe0_internal_xfer(struct renesas_usb3 *usb3,
  885. void *tx_data, size_t len,
  886. void (*complete)(struct usb_ep *ep,
  887. struct usb_request *req))
  888. {
  889. struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
  890. if (tx_data)
  891. memcpy(usb3->ep0_buf, tx_data,
  892. min_t(size_t, len, USB3_EP0_BUF_SIZE));
  893. usb3->ep0_req->buf = &usb3->ep0_buf;
  894. usb3->ep0_req->length = len;
  895. usb3->ep0_req->complete = complete;
  896. renesas_usb3_ep_queue(&usb3_ep->ep, usb3->ep0_req, GFP_ATOMIC);
  897. }
  898. static void usb3_pipe0_get_status_completion(struct usb_ep *ep,
  899. struct usb_request *req)
  900. {
  901. }
  902. static bool usb3_std_req_get_status(struct renesas_usb3 *usb3,
  903. struct usb_ctrlrequest *ctrl)
  904. {
  905. bool stall = false;
  906. struct renesas_usb3_ep *usb3_ep;
  907. int num;
  908. u16 status = 0;
  909. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  910. case USB_RECIP_DEVICE:
  911. if (usb3->gadget.is_selfpowered)
  912. status |= 1 << USB_DEVICE_SELF_POWERED;
  913. if (usb3->gadget.speed == USB_SPEED_SUPER)
  914. status |= usb3_feature_get_un_enabled(usb3);
  915. break;
  916. case USB_RECIP_INTERFACE:
  917. break;
  918. case USB_RECIP_ENDPOINT:
  919. num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
  920. usb3_ep = usb3_get_ep(usb3, num);
  921. if (usb3_ep->halt)
  922. status |= 1 << USB_ENDPOINT_HALT;
  923. break;
  924. default:
  925. stall = true;
  926. break;
  927. }
  928. if (!stall) {
  929. status = cpu_to_le16(status);
  930. dev_dbg(usb3_to_dev(usb3), "get_status: req = %p\n",
  931. usb_req_to_usb3_req(usb3->ep0_req));
  932. usb3_pipe0_internal_xfer(usb3, &status, sizeof(status),
  933. usb3_pipe0_get_status_completion);
  934. }
  935. return stall;
  936. }
  937. static bool usb3_std_req_feature_device(struct renesas_usb3 *usb3,
  938. struct usb_ctrlrequest *ctrl, bool set)
  939. {
  940. bool stall = true;
  941. u16 w_value = le16_to_cpu(ctrl->wValue);
  942. switch (w_value) {
  943. case USB_DEVICE_TEST_MODE:
  944. if (!set)
  945. break;
  946. usb3->test_mode = le16_to_cpu(ctrl->wIndex) >> 8;
  947. stall = false;
  948. break;
  949. case USB_DEVICE_U1_ENABLE:
  950. case USB_DEVICE_U2_ENABLE:
  951. if (usb3->gadget.speed != USB_SPEED_SUPER)
  952. break;
  953. if (w_value == USB_DEVICE_U1_ENABLE)
  954. usb3_feature_u1_enable(usb3, set);
  955. if (w_value == USB_DEVICE_U2_ENABLE)
  956. usb3_feature_u2_enable(usb3, set);
  957. stall = false;
  958. break;
  959. default:
  960. break;
  961. }
  962. return stall;
  963. }
  964. static int usb3_set_halt_p0(struct renesas_usb3_ep *usb3_ep, bool halt)
  965. {
  966. struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
  967. if (unlikely(usb3_ep->num))
  968. return -EINVAL;
  969. usb3_ep->halt = halt;
  970. if (halt)
  971. usb3_set_p0_con_stall(usb3);
  972. else
  973. usb3_set_p0_con_stop(usb3);
  974. return 0;
  975. }
  976. static int usb3_set_halt_pn(struct renesas_usb3_ep *usb3_ep, bool halt,
  977. bool is_clear_feature)
  978. {
  979. struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
  980. unsigned long flags;
  981. spin_lock_irqsave(&usb3->lock, flags);
  982. if (!usb3_pn_change(usb3, usb3_ep->num)) {
  983. usb3_ep->halt = halt;
  984. if (halt) {
  985. usb3_pn_stall(usb3);
  986. } else if (!is_clear_feature || !usb3_ep->wedge) {
  987. usb3_pn_con_clear(usb3);
  988. usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
  989. usb3_pn_stop(usb3);
  990. }
  991. }
  992. spin_unlock_irqrestore(&usb3->lock, flags);
  993. return 0;
  994. }
  995. static int usb3_set_halt(struct renesas_usb3_ep *usb3_ep, bool halt,
  996. bool is_clear_feature)
  997. {
  998. int ret = 0;
  999. if (halt && usb3_ep->started)
  1000. return -EAGAIN;
  1001. if (usb3_ep->num)
  1002. ret = usb3_set_halt_pn(usb3_ep, halt, is_clear_feature);
  1003. else
  1004. ret = usb3_set_halt_p0(usb3_ep, halt);
  1005. return ret;
  1006. }
  1007. static bool usb3_std_req_feature_endpoint(struct renesas_usb3 *usb3,
  1008. struct usb_ctrlrequest *ctrl,
  1009. bool set)
  1010. {
  1011. int num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
  1012. struct renesas_usb3_ep *usb3_ep;
  1013. struct renesas_usb3_request *usb3_req;
  1014. if (le16_to_cpu(ctrl->wValue) != USB_ENDPOINT_HALT)
  1015. return true; /* stall */
  1016. usb3_ep = usb3_get_ep(usb3, num);
  1017. usb3_set_halt(usb3_ep, set, true);
  1018. /* Restarts a queue if clear feature */
  1019. if (!set) {
  1020. usb3_ep->started = false;
  1021. usb3_req = usb3_get_request(usb3_ep);
  1022. if (usb3_req)
  1023. usb3_start_pipen(usb3_ep, usb3_req);
  1024. }
  1025. return false;
  1026. }
  1027. static bool usb3_std_req_feature(struct renesas_usb3 *usb3,
  1028. struct usb_ctrlrequest *ctrl, bool set)
  1029. {
  1030. bool stall = false;
  1031. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  1032. case USB_RECIP_DEVICE:
  1033. stall = usb3_std_req_feature_device(usb3, ctrl, set);
  1034. break;
  1035. case USB_RECIP_INTERFACE:
  1036. break;
  1037. case USB_RECIP_ENDPOINT:
  1038. stall = usb3_std_req_feature_endpoint(usb3, ctrl, set);
  1039. break;
  1040. default:
  1041. stall = true;
  1042. break;
  1043. }
  1044. if (!stall)
  1045. usb3_set_p0_con_for_no_data(usb3);
  1046. return stall;
  1047. }
  1048. static void usb3_pipe0_set_sel_completion(struct usb_ep *ep,
  1049. struct usb_request *req)
  1050. {
  1051. /* TODO */
  1052. }
  1053. static bool usb3_std_req_set_sel(struct renesas_usb3 *usb3,
  1054. struct usb_ctrlrequest *ctrl)
  1055. {
  1056. u16 w_length = le16_to_cpu(ctrl->wLength);
  1057. if (w_length != 6)
  1058. return true; /* stall */
  1059. dev_dbg(usb3_to_dev(usb3), "set_sel: req = %p\n",
  1060. usb_req_to_usb3_req(usb3->ep0_req));
  1061. usb3_pipe0_internal_xfer(usb3, NULL, 6, usb3_pipe0_set_sel_completion);
  1062. return false;
  1063. }
  1064. static bool usb3_std_req_set_configuration(struct renesas_usb3 *usb3,
  1065. struct usb_ctrlrequest *ctrl)
  1066. {
  1067. if (ctrl->wValue > 0)
  1068. usb3_set_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
  1069. else
  1070. usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON);
  1071. return false;
  1072. }
  1073. /**
  1074. * usb3_handle_standard_request - handle some standard requests
  1075. * @usb3: the renesas_usb3 pointer
  1076. * @ctrl: a pointer of setup data
  1077. *
  1078. * Returns true if this function handled a standard request
  1079. */
  1080. static bool usb3_handle_standard_request(struct renesas_usb3 *usb3,
  1081. struct usb_ctrlrequest *ctrl)
  1082. {
  1083. bool ret = false;
  1084. bool stall = false;
  1085. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  1086. switch (ctrl->bRequest) {
  1087. case USB_REQ_SET_ADDRESS:
  1088. stall = usb3_std_req_set_address(usb3, ctrl);
  1089. ret = true;
  1090. break;
  1091. case USB_REQ_GET_STATUS:
  1092. stall = usb3_std_req_get_status(usb3, ctrl);
  1093. ret = true;
  1094. break;
  1095. case USB_REQ_CLEAR_FEATURE:
  1096. stall = usb3_std_req_feature(usb3, ctrl, false);
  1097. ret = true;
  1098. break;
  1099. case USB_REQ_SET_FEATURE:
  1100. stall = usb3_std_req_feature(usb3, ctrl, true);
  1101. ret = true;
  1102. break;
  1103. case USB_REQ_SET_SEL:
  1104. stall = usb3_std_req_set_sel(usb3, ctrl);
  1105. ret = true;
  1106. break;
  1107. case USB_REQ_SET_ISOCH_DELAY:
  1108. /* This hardware doesn't support Isochronous xfer */
  1109. stall = true;
  1110. ret = true;
  1111. break;
  1112. case USB_REQ_SET_CONFIGURATION:
  1113. usb3_std_req_set_configuration(usb3, ctrl);
  1114. break;
  1115. default:
  1116. break;
  1117. }
  1118. }
  1119. if (stall)
  1120. usb3_set_p0_con_stall(usb3);
  1121. return ret;
  1122. }
  1123. static int usb3_p0_con_clear_buffer(struct renesas_usb3 *usb3)
  1124. {
  1125. usb3_set_bit(usb3, P0_CON_BCLR, USB3_P0_CON);
  1126. return usb3_wait(usb3, USB3_P0_CON, P0_CON_BCLR, 0);
  1127. }
  1128. static void usb3_irq_epc_pipe0_setup(struct renesas_usb3 *usb3)
  1129. {
  1130. struct usb_ctrlrequest ctrl;
  1131. struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
  1132. /* Call giveback function if previous transfer is not completed */
  1133. if (usb3_ep->started)
  1134. usb3_request_done(usb3_ep, usb3_get_request(usb3_ep),
  1135. -ECONNRESET);
  1136. usb3_p0_con_clear_buffer(usb3);
  1137. usb3_get_setup_data(usb3, &ctrl);
  1138. if (!usb3_handle_standard_request(usb3, &ctrl))
  1139. if (usb3->driver->setup(&usb3->gadget, &ctrl) < 0)
  1140. usb3_set_p0_con_stall(usb3);
  1141. }
  1142. static void usb3_irq_epc_pipe0_bfrdy(struct renesas_usb3 *usb3)
  1143. {
  1144. struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0);
  1145. struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
  1146. if (!usb3_req)
  1147. return;
  1148. usb3_p0_xfer(usb3_ep, usb3_req);
  1149. }
  1150. static void usb3_irq_epc_pipe0(struct renesas_usb3 *usb3)
  1151. {
  1152. u32 p0_int_sta = usb3_read(usb3, USB3_P0_INT_STA);
  1153. p0_int_sta &= usb3_read(usb3, USB3_P0_INT_ENA);
  1154. usb3_write(usb3, p0_int_sta, USB3_P0_INT_STA);
  1155. if (p0_int_sta & P0_INT_STSED)
  1156. usb3_irq_epc_pipe0_status_end(usb3);
  1157. if (p0_int_sta & P0_INT_SETUP)
  1158. usb3_irq_epc_pipe0_setup(usb3);
  1159. if (p0_int_sta & P0_INT_BFRDY)
  1160. usb3_irq_epc_pipe0_bfrdy(usb3);
  1161. }
  1162. static void usb3_request_done_pipen(struct renesas_usb3 *usb3,
  1163. struct renesas_usb3_ep *usb3_ep,
  1164. struct renesas_usb3_request *usb3_req,
  1165. int status)
  1166. {
  1167. usb3_pn_stop(usb3);
  1168. usb3_disable_pipe_irq(usb3, usb3_ep->num);
  1169. usb3_request_done(usb3_ep, usb3_req, status);
  1170. /* get next usb3_req */
  1171. usb3_req = usb3_get_request(usb3_ep);
  1172. if (usb3_req)
  1173. usb3_start_pipen(usb3_ep, usb3_req);
  1174. }
  1175. static void usb3_irq_epc_pipen_lsttr(struct renesas_usb3 *usb3, int num)
  1176. {
  1177. struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num);
  1178. struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
  1179. if (!usb3_req)
  1180. return;
  1181. if (usb3_ep->dir_in) {
  1182. dev_dbg(usb3_to_dev(usb3), "%s: len = %u, actual = %u\n",
  1183. __func__, usb3_req->req.length, usb3_req->req.actual);
  1184. usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0);
  1185. }
  1186. }
  1187. static void usb3_irq_epc_pipen_bfrdy(struct renesas_usb3 *usb3, int num)
  1188. {
  1189. struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num);
  1190. struct renesas_usb3_request *usb3_req = usb3_get_request(usb3_ep);
  1191. if (!usb3_req)
  1192. return;
  1193. if (usb3_ep->dir_in) {
  1194. /* Do not stop the IN pipe here to detect LSTTR interrupt */
  1195. if (!usb3_write_pipe(usb3_ep, usb3_req, USB3_PN_WRITE))
  1196. usb3_clear_bit(usb3, PN_INT_BFRDY, USB3_PN_INT_ENA);
  1197. } else {
  1198. if (!usb3_read_pipe(usb3_ep, usb3_req, USB3_PN_READ))
  1199. usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0);
  1200. }
  1201. }
  1202. static void usb3_irq_epc_pipen(struct renesas_usb3 *usb3, int num)
  1203. {
  1204. u32 pn_int_sta;
  1205. if (usb3_pn_change(usb3, num) < 0)
  1206. return;
  1207. pn_int_sta = usb3_read(usb3, USB3_PN_INT_STA);
  1208. pn_int_sta &= usb3_read(usb3, USB3_PN_INT_ENA);
  1209. usb3_write(usb3, pn_int_sta, USB3_PN_INT_STA);
  1210. if (pn_int_sta & PN_INT_LSTTR)
  1211. usb3_irq_epc_pipen_lsttr(usb3, num);
  1212. if (pn_int_sta & PN_INT_BFRDY)
  1213. usb3_irq_epc_pipen_bfrdy(usb3, num);
  1214. }
  1215. static void usb3_irq_epc_int_2(struct renesas_usb3 *usb3, u32 int_sta_2)
  1216. {
  1217. int i;
  1218. for (i = 0; i < usb3->num_usb3_eps; i++) {
  1219. if (int_sta_2 & USB_INT_2_PIPE(i)) {
  1220. if (!i)
  1221. usb3_irq_epc_pipe0(usb3);
  1222. else
  1223. usb3_irq_epc_pipen(usb3, i);
  1224. }
  1225. }
  1226. }
  1227. static void usb3_irq_epc(struct renesas_usb3 *usb3)
  1228. {
  1229. u32 int_sta_1 = usb3_read(usb3, USB3_USB_INT_STA_1);
  1230. u32 int_sta_2 = usb3_read(usb3, USB3_USB_INT_STA_2);
  1231. int_sta_1 &= usb3_read(usb3, USB3_USB_INT_ENA_1);
  1232. if (int_sta_1) {
  1233. usb3_write(usb3, int_sta_1, USB3_USB_INT_STA_1);
  1234. usb3_irq_epc_int_1(usb3, int_sta_1);
  1235. }
  1236. int_sta_2 &= usb3_read(usb3, USB3_USB_INT_ENA_2);
  1237. if (int_sta_2)
  1238. usb3_irq_epc_int_2(usb3, int_sta_2);
  1239. }
  1240. static irqreturn_t renesas_usb3_irq(int irq, void *_usb3)
  1241. {
  1242. struct renesas_usb3 *usb3 = _usb3;
  1243. irqreturn_t ret = IRQ_NONE;
  1244. u32 axi_int_sta = usb3_read(usb3, USB3_AXI_INT_STA);
  1245. if (axi_int_sta & AXI_INT_EPCINT) {
  1246. usb3_irq_epc(usb3);
  1247. ret = IRQ_HANDLED;
  1248. }
  1249. return ret;
  1250. }
  1251. static void usb3_write_pn_mod(struct renesas_usb3_ep *usb3_ep,
  1252. const struct usb_endpoint_descriptor *desc)
  1253. {
  1254. struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
  1255. u32 val = 0;
  1256. val |= usb3_ep->dir_in ? PN_MOD_DIR : 0;
  1257. val |= PN_MOD_TYPE(usb_endpoint_type(desc));
  1258. val |= PN_MOD_EPNUM(usb_endpoint_num(desc));
  1259. usb3_write(usb3, val, USB3_PN_MOD);
  1260. }
  1261. static u32 usb3_calc_ramarea(int ram_size)
  1262. {
  1263. WARN_ON(ram_size > SZ_16K);
  1264. if (ram_size <= SZ_1K)
  1265. return PN_RAMMAP_RAMAREA_1KB;
  1266. else if (ram_size <= SZ_2K)
  1267. return PN_RAMMAP_RAMAREA_2KB;
  1268. else if (ram_size <= SZ_4K)
  1269. return PN_RAMMAP_RAMAREA_4KB;
  1270. else if (ram_size <= SZ_8K)
  1271. return PN_RAMMAP_RAMAREA_8KB;
  1272. else
  1273. return PN_RAMMAP_RAMAREA_16KB;
  1274. }
  1275. static u32 usb3_calc_rammap_val(struct renesas_usb3_ep *usb3_ep,
  1276. const struct usb_endpoint_descriptor *desc)
  1277. {
  1278. return usb3_ep->rammap_val | PN_RAMMAP_MPKT(usb_endpoint_maxp(desc));
  1279. }
  1280. static int usb3_enable_pipe_n(struct renesas_usb3_ep *usb3_ep,
  1281. const struct usb_endpoint_descriptor *desc)
  1282. {
  1283. struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
  1284. unsigned long flags;
  1285. usb3_ep->dir_in = usb_endpoint_dir_in(desc);
  1286. spin_lock_irqsave(&usb3->lock, flags);
  1287. if (!usb3_pn_change(usb3, usb3_ep->num)) {
  1288. usb3_write_pn_mod(usb3_ep, desc);
  1289. usb3_write(usb3, usb3_calc_rammap_val(usb3_ep, desc),
  1290. USB3_PN_RAMMAP);
  1291. usb3_pn_con_clear(usb3);
  1292. usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
  1293. }
  1294. spin_unlock_irqrestore(&usb3->lock, flags);
  1295. return 0;
  1296. }
  1297. static int usb3_disable_pipe_n(struct renesas_usb3_ep *usb3_ep)
  1298. {
  1299. struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
  1300. unsigned long flags;
  1301. usb3_ep->halt = false;
  1302. spin_lock_irqsave(&usb3->lock, flags);
  1303. if (!usb3_pn_change(usb3, usb3_ep->num)) {
  1304. usb3_write(usb3, 0, USB3_PN_RAMMAP);
  1305. usb3_clear_bit(usb3, PN_CON_EN, USB3_PN_CON);
  1306. }
  1307. spin_unlock_irqrestore(&usb3->lock, flags);
  1308. return 0;
  1309. }
  1310. /*------- usb_ep_ops -----------------------------------------------------*/
  1311. static int renesas_usb3_ep_enable(struct usb_ep *_ep,
  1312. const struct usb_endpoint_descriptor *desc)
  1313. {
  1314. struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
  1315. return usb3_enable_pipe_n(usb3_ep, desc);
  1316. }
  1317. static int renesas_usb3_ep_disable(struct usb_ep *_ep)
  1318. {
  1319. struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
  1320. struct renesas_usb3_request *usb3_req;
  1321. do {
  1322. usb3_req = usb3_get_request(usb3_ep);
  1323. if (!usb3_req)
  1324. break;
  1325. usb3_request_done(usb3_ep, usb3_req, -ESHUTDOWN);
  1326. } while (1);
  1327. return usb3_disable_pipe_n(usb3_ep);
  1328. }
  1329. static struct usb_request *__renesas_usb3_ep_alloc_request(gfp_t gfp_flags)
  1330. {
  1331. struct renesas_usb3_request *usb3_req;
  1332. usb3_req = kzalloc(sizeof(struct renesas_usb3_request), gfp_flags);
  1333. if (!usb3_req)
  1334. return NULL;
  1335. INIT_LIST_HEAD(&usb3_req->queue);
  1336. return &usb3_req->req;
  1337. }
  1338. static void __renesas_usb3_ep_free_request(struct usb_request *_req)
  1339. {
  1340. struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
  1341. kfree(usb3_req);
  1342. }
  1343. static struct usb_request *renesas_usb3_ep_alloc_request(struct usb_ep *_ep,
  1344. gfp_t gfp_flags)
  1345. {
  1346. return __renesas_usb3_ep_alloc_request(gfp_flags);
  1347. }
  1348. static void renesas_usb3_ep_free_request(struct usb_ep *_ep,
  1349. struct usb_request *_req)
  1350. {
  1351. __renesas_usb3_ep_free_request(_req);
  1352. }
  1353. static int renesas_usb3_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  1354. {
  1355. struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
  1356. struct renesas_usb3_request *usb3_req = usb_req_to_usb3_req(_req);
  1357. struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
  1358. dev_dbg(usb3_to_dev(usb3), "ep_dequeue: ep%2d, %u\n", usb3_ep->num,
  1359. _req->length);
  1360. usb3_request_done_pipen(usb3, usb3_ep, usb3_req, -ECONNRESET);
  1361. return 0;
  1362. }
  1363. static int renesas_usb3_ep_set_halt(struct usb_ep *_ep, int value)
  1364. {
  1365. return usb3_set_halt(usb_ep_to_usb3_ep(_ep), !!value, false);
  1366. }
  1367. static int renesas_usb3_ep_set_wedge(struct usb_ep *_ep)
  1368. {
  1369. struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
  1370. usb3_ep->wedge = true;
  1371. return usb3_set_halt(usb3_ep, true, false);
  1372. }
  1373. static void renesas_usb3_ep_fifo_flush(struct usb_ep *_ep)
  1374. {
  1375. struct renesas_usb3_ep *usb3_ep = usb_ep_to_usb3_ep(_ep);
  1376. struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep);
  1377. unsigned long flags;
  1378. if (usb3_ep->num) {
  1379. spin_lock_irqsave(&usb3->lock, flags);
  1380. if (!usb3_pn_change(usb3, usb3_ep->num)) {
  1381. usb3_pn_con_clear(usb3);
  1382. usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON);
  1383. }
  1384. spin_unlock_irqrestore(&usb3->lock, flags);
  1385. } else {
  1386. usb3_p0_con_clear_buffer(usb3);
  1387. }
  1388. }
  1389. static struct usb_ep_ops renesas_usb3_ep_ops = {
  1390. .enable = renesas_usb3_ep_enable,
  1391. .disable = renesas_usb3_ep_disable,
  1392. .alloc_request = renesas_usb3_ep_alloc_request,
  1393. .free_request = renesas_usb3_ep_free_request,
  1394. .queue = renesas_usb3_ep_queue,
  1395. .dequeue = renesas_usb3_ep_dequeue,
  1396. .set_halt = renesas_usb3_ep_set_halt,
  1397. .set_wedge = renesas_usb3_ep_set_wedge,
  1398. .fifo_flush = renesas_usb3_ep_fifo_flush,
  1399. };
  1400. /*------- usb_gadget_ops -------------------------------------------------*/
  1401. static int renesas_usb3_start(struct usb_gadget *gadget,
  1402. struct usb_gadget_driver *driver)
  1403. {
  1404. struct renesas_usb3 *usb3;
  1405. if (!driver || driver->max_speed < USB_SPEED_FULL ||
  1406. !driver->setup)
  1407. return -EINVAL;
  1408. usb3 = gadget_to_renesas_usb3(gadget);
  1409. /* hook up the driver */
  1410. usb3->driver = driver;
  1411. renesas_usb3_init_controller(usb3);
  1412. return 0;
  1413. }
  1414. static int renesas_usb3_stop(struct usb_gadget *gadget)
  1415. {
  1416. struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget);
  1417. unsigned long flags;
  1418. spin_lock_irqsave(&usb3->lock, flags);
  1419. usb3->softconnect = false;
  1420. usb3->gadget.speed = USB_SPEED_UNKNOWN;
  1421. usb3->driver = NULL;
  1422. renesas_usb3_stop_controller(usb3);
  1423. spin_unlock_irqrestore(&usb3->lock, flags);
  1424. return 0;
  1425. }
  1426. static int renesas_usb3_get_frame(struct usb_gadget *_gadget)
  1427. {
  1428. return -EOPNOTSUPP;
  1429. }
  1430. static int renesas_usb3_pullup(struct usb_gadget *gadget, int is_on)
  1431. {
  1432. struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget);
  1433. usb3->softconnect = !!is_on;
  1434. return 0;
  1435. }
  1436. static int renesas_usb3_set_selfpowered(struct usb_gadget *gadget, int is_self)
  1437. {
  1438. gadget->is_selfpowered = !!is_self;
  1439. return 0;
  1440. }
  1441. static const struct usb_gadget_ops renesas_usb3_gadget_ops = {
  1442. .get_frame = renesas_usb3_get_frame,
  1443. .udc_start = renesas_usb3_start,
  1444. .udc_stop = renesas_usb3_stop,
  1445. .pullup = renesas_usb3_pullup,
  1446. .set_selfpowered = renesas_usb3_set_selfpowered,
  1447. };
  1448. /*------- platform_driver ------------------------------------------------*/
  1449. static int renesas_usb3_remove(struct platform_device *pdev)
  1450. {
  1451. struct renesas_usb3 *usb3 = platform_get_drvdata(pdev);
  1452. pm_runtime_put(&pdev->dev);
  1453. pm_runtime_disable(&pdev->dev);
  1454. usb_del_gadget_udc(&usb3->gadget);
  1455. __renesas_usb3_ep_free_request(usb3->ep0_req);
  1456. return 0;
  1457. }
  1458. static int renesas_usb3_init_ep(struct renesas_usb3 *usb3, struct device *dev,
  1459. const struct renesas_usb3_priv *priv)
  1460. {
  1461. struct renesas_usb3_ep *usb3_ep;
  1462. int i;
  1463. /* calculate num_usb3_eps from renesas_usb3_priv */
  1464. usb3->num_usb3_eps = priv->ramsize_per_ramif * priv->num_ramif * 2 /
  1465. priv->ramsize_per_pipe + 1;
  1466. if (usb3->num_usb3_eps > USB3_MAX_NUM_PIPES)
  1467. usb3->num_usb3_eps = USB3_MAX_NUM_PIPES;
  1468. usb3->usb3_ep = devm_kzalloc(dev, sizeof(*usb3_ep) * usb3->num_usb3_eps,
  1469. GFP_KERNEL);
  1470. if (!usb3->usb3_ep)
  1471. return -ENOMEM;
  1472. dev_dbg(dev, "%s: num_usb3_eps = %d\n", __func__, usb3->num_usb3_eps);
  1473. /*
  1474. * This driver prepares pipes as the followings:
  1475. * - odd pipes = IN pipe
  1476. * - even pipes = OUT pipe (except pipe 0)
  1477. */
  1478. usb3_for_each_ep(usb3_ep, usb3, i) {
  1479. snprintf(usb3_ep->ep_name, sizeof(usb3_ep->ep_name), "ep%d", i);
  1480. usb3_ep->usb3 = usb3;
  1481. usb3_ep->num = i;
  1482. usb3_ep->ep.name = usb3_ep->ep_name;
  1483. usb3_ep->ep.ops = &renesas_usb3_ep_ops;
  1484. INIT_LIST_HEAD(&usb3_ep->queue);
  1485. INIT_LIST_HEAD(&usb3_ep->ep.ep_list);
  1486. if (!i) {
  1487. /* for control pipe */
  1488. usb3->gadget.ep0 = &usb3_ep->ep;
  1489. usb_ep_set_maxpacket_limit(&usb3_ep->ep,
  1490. USB3_EP0_HSFS_MAX_PACKET_SIZE);
  1491. usb3_ep->ep.caps.type_control = true;
  1492. usb3_ep->ep.caps.dir_in = true;
  1493. usb3_ep->ep.caps.dir_out = true;
  1494. continue;
  1495. }
  1496. /* for bulk or interrupt pipe */
  1497. usb_ep_set_maxpacket_limit(&usb3_ep->ep, ~0);
  1498. list_add_tail(&usb3_ep->ep.ep_list, &usb3->gadget.ep_list);
  1499. usb3_ep->ep.caps.type_bulk = true;
  1500. usb3_ep->ep.caps.type_int = true;
  1501. if (i & 1)
  1502. usb3_ep->ep.caps.dir_in = true;
  1503. else
  1504. usb3_ep->ep.caps.dir_out = true;
  1505. }
  1506. return 0;
  1507. }
  1508. static void renesas_usb3_init_ram(struct renesas_usb3 *usb3, struct device *dev,
  1509. const struct renesas_usb3_priv *priv)
  1510. {
  1511. struct renesas_usb3_ep *usb3_ep;
  1512. int i;
  1513. u32 ramif[2], basead[2]; /* index 0 = for IN pipes */
  1514. u32 *cur_ramif, *cur_basead;
  1515. u32 val;
  1516. memset(ramif, 0, sizeof(ramif));
  1517. memset(basead, 0, sizeof(basead));
  1518. /*
  1519. * This driver prepares pipes as the followings:
  1520. * - all pipes = the same size as "ramsize_per_pipe"
  1521. * Please refer to the "Method of Specifying RAM Mapping"
  1522. */
  1523. usb3_for_each_ep(usb3_ep, usb3, i) {
  1524. if (!i)
  1525. continue; /* out of scope if ep num = 0 */
  1526. if (usb3_ep->ep.caps.dir_in) {
  1527. cur_ramif = &ramif[0];
  1528. cur_basead = &basead[0];
  1529. } else {
  1530. cur_ramif = &ramif[1];
  1531. cur_basead = &basead[1];
  1532. }
  1533. if (*cur_basead > priv->ramsize_per_ramif)
  1534. continue; /* out of memory for IN or OUT pipe */
  1535. /* calculate rammap_val */
  1536. val = PN_RAMMAP_RAMIF(*cur_ramif);
  1537. val |= usb3_calc_ramarea(priv->ramsize_per_pipe);
  1538. val |= PN_RAMMAP_BASEAD(*cur_basead);
  1539. usb3_ep->rammap_val = val;
  1540. dev_dbg(dev, "ep%2d: val = %08x, ramif = %d, base = %x\n",
  1541. i, val, *cur_ramif, *cur_basead);
  1542. /* update current ramif */
  1543. if (*cur_ramif + 1 == priv->num_ramif) {
  1544. *cur_ramif = 0;
  1545. *cur_basead += priv->ramsize_per_pipe;
  1546. } else {
  1547. (*cur_ramif)++;
  1548. }
  1549. }
  1550. }
  1551. static const struct renesas_usb3_priv renesas_usb3_priv_r8a7795 = {
  1552. .ramsize_per_ramif = SZ_16K,
  1553. .num_ramif = 2,
  1554. .ramsize_per_pipe = SZ_4K,
  1555. .workaround_for_vbus = true,
  1556. };
  1557. static const struct of_device_id usb3_of_match[] = {
  1558. {
  1559. .compatible = "renesas,r8a7795-usb3-peri",
  1560. .data = &renesas_usb3_priv_r8a7795,
  1561. },
  1562. { },
  1563. };
  1564. MODULE_DEVICE_TABLE(of, usb3_of_match);
  1565. static int renesas_usb3_probe(struct platform_device *pdev)
  1566. {
  1567. struct renesas_usb3 *usb3;
  1568. struct resource *res;
  1569. const struct of_device_id *match;
  1570. int irq, ret;
  1571. const struct renesas_usb3_priv *priv;
  1572. match = of_match_node(usb3_of_match, pdev->dev.of_node);
  1573. if (!match)
  1574. return -ENODEV;
  1575. priv = match->data;
  1576. irq = platform_get_irq(pdev, 0);
  1577. if (irq < 0)
  1578. return -ENODEV;
  1579. usb3 = devm_kzalloc(&pdev->dev, sizeof(*usb3), GFP_KERNEL);
  1580. if (!usb3)
  1581. return -ENOMEM;
  1582. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1583. usb3->reg = devm_ioremap_resource(&pdev->dev, res);
  1584. if (IS_ERR(usb3->reg))
  1585. return PTR_ERR(usb3->reg);
  1586. platform_set_drvdata(pdev, usb3);
  1587. spin_lock_init(&usb3->lock);
  1588. usb3->gadget.ops = &renesas_usb3_gadget_ops;
  1589. usb3->gadget.name = udc_name;
  1590. usb3->gadget.max_speed = USB_SPEED_SUPER;
  1591. INIT_LIST_HEAD(&usb3->gadget.ep_list);
  1592. ret = renesas_usb3_init_ep(usb3, &pdev->dev, priv);
  1593. if (ret < 0)
  1594. return ret;
  1595. renesas_usb3_init_ram(usb3, &pdev->dev, priv);
  1596. ret = devm_request_irq(&pdev->dev, irq, renesas_usb3_irq, 0,
  1597. dev_name(&pdev->dev), usb3);
  1598. if (ret < 0)
  1599. return ret;
  1600. /* for ep0 handling */
  1601. usb3->ep0_req = __renesas_usb3_ep_alloc_request(GFP_KERNEL);
  1602. if (!usb3->ep0_req)
  1603. return -ENOMEM;
  1604. ret = usb_add_gadget_udc(&pdev->dev, &usb3->gadget);
  1605. if (ret < 0)
  1606. goto err_add_udc;
  1607. usb3->workaround_for_vbus = priv->workaround_for_vbus;
  1608. pm_runtime_enable(&pdev->dev);
  1609. pm_runtime_get_sync(&pdev->dev);
  1610. dev_info(&pdev->dev, "probed\n");
  1611. return 0;
  1612. err_add_udc:
  1613. __renesas_usb3_ep_free_request(usb3->ep0_req);
  1614. return ret;
  1615. }
  1616. static struct platform_driver renesas_usb3_driver = {
  1617. .probe = renesas_usb3_probe,
  1618. .remove = renesas_usb3_remove,
  1619. .driver = {
  1620. .name = (char *)udc_name,
  1621. .of_match_table = of_match_ptr(usb3_of_match),
  1622. },
  1623. };
  1624. module_platform_driver(renesas_usb3_driver);
  1625. MODULE_DESCRIPTION("Renesas USB3.0 Peripheral driver");
  1626. MODULE_LICENSE("GPL v2");
  1627. MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");
  1628. MODULE_ALIAS("platform:renesas_usb3");