mtu3_core.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mtu3_core.c - hardware access layer and gadget init/exit of
  4. * MediaTek usb3 Dual-Role Controller Driver
  5. *
  6. * Copyright (C) 2016 MediaTek Inc.
  7. *
  8. * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
  9. */
  10. #include <linux/dma-mapping.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/platform_device.h>
  16. #include "mtu3.h"
  17. static int ep_fifo_alloc(struct mtu3_ep *mep, u32 seg_size)
  18. {
  19. struct mtu3_fifo_info *fifo = mep->fifo;
  20. u32 num_bits = DIV_ROUND_UP(seg_size, MTU3_EP_FIFO_UNIT);
  21. u32 start_bit;
  22. /* ensure that @mep->fifo_seg_size is power of two */
  23. num_bits = roundup_pow_of_two(num_bits);
  24. if (num_bits > fifo->limit)
  25. return -EINVAL;
  26. mep->fifo_seg_size = num_bits * MTU3_EP_FIFO_UNIT;
  27. num_bits = num_bits * (mep->slot + 1);
  28. start_bit = bitmap_find_next_zero_area(fifo->bitmap,
  29. fifo->limit, 0, num_bits, 0);
  30. if (start_bit >= fifo->limit)
  31. return -EOVERFLOW;
  32. bitmap_set(fifo->bitmap, start_bit, num_bits);
  33. mep->fifo_size = num_bits * MTU3_EP_FIFO_UNIT;
  34. mep->fifo_addr = fifo->base + MTU3_EP_FIFO_UNIT * start_bit;
  35. dev_dbg(mep->mtu->dev, "%s fifo:%#x/%#x, start_bit: %d\n",
  36. __func__, mep->fifo_seg_size, mep->fifo_size, start_bit);
  37. return mep->fifo_addr;
  38. }
  39. static void ep_fifo_free(struct mtu3_ep *mep)
  40. {
  41. struct mtu3_fifo_info *fifo = mep->fifo;
  42. u32 addr = mep->fifo_addr;
  43. u32 bits = mep->fifo_size / MTU3_EP_FIFO_UNIT;
  44. u32 start_bit;
  45. if (unlikely(addr < fifo->base || bits > fifo->limit))
  46. return;
  47. start_bit = (addr - fifo->base) / MTU3_EP_FIFO_UNIT;
  48. bitmap_clear(fifo->bitmap, start_bit, bits);
  49. mep->fifo_size = 0;
  50. mep->fifo_seg_size = 0;
  51. dev_dbg(mep->mtu->dev, "%s size:%#x/%#x, start_bit: %d\n",
  52. __func__, mep->fifo_seg_size, mep->fifo_size, start_bit);
  53. }
  54. /* enable/disable U3D SS function */
  55. static inline void mtu3_ss_func_set(struct mtu3 *mtu, bool enable)
  56. {
  57. /* If usb3_en==0, LTSSM will go to SS.Disable state */
  58. if (enable)
  59. mtu3_setbits(mtu->mac_base, U3D_USB3_CONFIG, USB3_EN);
  60. else
  61. mtu3_clrbits(mtu->mac_base, U3D_USB3_CONFIG, USB3_EN);
  62. dev_dbg(mtu->dev, "USB3_EN = %d\n", !!enable);
  63. }
  64. /* set/clear U3D HS device soft connect */
  65. static inline void mtu3_hs_softconn_set(struct mtu3 *mtu, bool enable)
  66. {
  67. if (enable) {
  68. mtu3_setbits(mtu->mac_base, U3D_POWER_MANAGEMENT,
  69. SOFT_CONN | SUSPENDM_ENABLE);
  70. } else {
  71. mtu3_clrbits(mtu->mac_base, U3D_POWER_MANAGEMENT,
  72. SOFT_CONN | SUSPENDM_ENABLE);
  73. }
  74. dev_dbg(mtu->dev, "SOFTCONN = %d\n", !!enable);
  75. }
  76. /* only port0 of U2/U3 supports device mode */
  77. static int mtu3_device_enable(struct mtu3 *mtu)
  78. {
  79. void __iomem *ibase = mtu->ippc_base;
  80. u32 check_clk = 0;
  81. mtu3_clrbits(ibase, U3D_SSUSB_IP_PW_CTRL2, SSUSB_IP_DEV_PDN);
  82. if (mtu->is_u3_ip) {
  83. check_clk = SSUSB_U3_MAC_RST_B_STS;
  84. mtu3_clrbits(ibase, SSUSB_U3_CTRL(0),
  85. (SSUSB_U3_PORT_DIS | SSUSB_U3_PORT_PDN |
  86. SSUSB_U3_PORT_HOST_SEL));
  87. }
  88. mtu3_clrbits(ibase, SSUSB_U2_CTRL(0),
  89. (SSUSB_U2_PORT_DIS | SSUSB_U2_PORT_PDN |
  90. SSUSB_U2_PORT_HOST_SEL));
  91. if (mtu->ssusb->dr_mode == USB_DR_MODE_OTG) {
  92. mtu3_setbits(ibase, SSUSB_U2_CTRL(0), SSUSB_U2_PORT_OTG_SEL);
  93. if (mtu->is_u3_ip)
  94. mtu3_setbits(ibase, SSUSB_U3_CTRL(0),
  95. SSUSB_U3_PORT_DUAL_MODE);
  96. }
  97. return ssusb_check_clocks(mtu->ssusb, check_clk);
  98. }
  99. static void mtu3_device_disable(struct mtu3 *mtu)
  100. {
  101. void __iomem *ibase = mtu->ippc_base;
  102. if (mtu->is_u3_ip)
  103. mtu3_setbits(ibase, SSUSB_U3_CTRL(0),
  104. (SSUSB_U3_PORT_DIS | SSUSB_U3_PORT_PDN));
  105. mtu3_setbits(ibase, SSUSB_U2_CTRL(0),
  106. SSUSB_U2_PORT_DIS | SSUSB_U2_PORT_PDN);
  107. if (mtu->ssusb->dr_mode == USB_DR_MODE_OTG)
  108. mtu3_clrbits(ibase, SSUSB_U2_CTRL(0), SSUSB_U2_PORT_OTG_SEL);
  109. mtu3_setbits(ibase, U3D_SSUSB_IP_PW_CTRL2, SSUSB_IP_DEV_PDN);
  110. }
  111. /* reset U3D's device module. */
  112. static void mtu3_device_reset(struct mtu3 *mtu)
  113. {
  114. void __iomem *ibase = mtu->ippc_base;
  115. mtu3_setbits(ibase, U3D_SSUSB_DEV_RST_CTRL, SSUSB_DEV_SW_RST);
  116. udelay(1);
  117. mtu3_clrbits(ibase, U3D_SSUSB_DEV_RST_CTRL, SSUSB_DEV_SW_RST);
  118. }
  119. /* disable all interrupts */
  120. static void mtu3_intr_disable(struct mtu3 *mtu)
  121. {
  122. void __iomem *mbase = mtu->mac_base;
  123. /* Disable level 1 interrupts */
  124. mtu3_writel(mbase, U3D_LV1IECR, ~0x0);
  125. /* Disable endpoint interrupts */
  126. mtu3_writel(mbase, U3D_EPIECR, ~0x0);
  127. }
  128. static void mtu3_intr_status_clear(struct mtu3 *mtu)
  129. {
  130. void __iomem *mbase = mtu->mac_base;
  131. /* Clear EP0 and Tx/Rx EPn interrupts status */
  132. mtu3_writel(mbase, U3D_EPISR, ~0x0);
  133. /* Clear U2 USB common interrupts status */
  134. mtu3_writel(mbase, U3D_COMMON_USB_INTR, ~0x0);
  135. /* Clear U3 LTSSM interrupts status */
  136. mtu3_writel(mbase, U3D_LTSSM_INTR, ~0x0);
  137. /* Clear speed change interrupt status */
  138. mtu3_writel(mbase, U3D_DEV_LINK_INTR, ~0x0);
  139. }
  140. /* enable system global interrupt */
  141. static void mtu3_intr_enable(struct mtu3 *mtu)
  142. {
  143. void __iomem *mbase = mtu->mac_base;
  144. u32 value;
  145. /*Enable level 1 interrupts (BMU, QMU, MAC3, DMA, MAC2, EPCTL) */
  146. value = BMU_INTR | QMU_INTR | MAC3_INTR | MAC2_INTR | EP_CTRL_INTR;
  147. mtu3_writel(mbase, U3D_LV1IESR, value);
  148. /* Enable U2 common USB interrupts */
  149. value = SUSPEND_INTR | RESUME_INTR | RESET_INTR | LPM_RESUME_INTR;
  150. mtu3_writel(mbase, U3D_COMMON_USB_INTR_ENABLE, value);
  151. if (mtu->is_u3_ip) {
  152. /* Enable U3 LTSSM interrupts */
  153. value = HOT_RST_INTR | WARM_RST_INTR |
  154. ENTER_U3_INTR | EXIT_U3_INTR;
  155. mtu3_writel(mbase, U3D_LTSSM_INTR_ENABLE, value);
  156. }
  157. /* Enable QMU interrupts. */
  158. value = TXQ_CSERR_INT | TXQ_LENERR_INT | RXQ_CSERR_INT |
  159. RXQ_LENERR_INT | RXQ_ZLPERR_INT;
  160. mtu3_writel(mbase, U3D_QIESR1, value);
  161. /* Enable speed change interrupt */
  162. mtu3_writel(mbase, U3D_DEV_LINK_INTR_ENABLE, SSUSB_DEV_SPEED_CHG_INTR);
  163. }
  164. /* reset: u2 - data toggle, u3 - SeqN, flow control status etc */
  165. static void mtu3_ep_reset(struct mtu3_ep *mep)
  166. {
  167. struct mtu3 *mtu = mep->mtu;
  168. u32 rst_bit = EP_RST(mep->is_in, mep->epnum);
  169. mtu3_setbits(mtu->mac_base, U3D_EP_RST, rst_bit);
  170. mtu3_clrbits(mtu->mac_base, U3D_EP_RST, rst_bit);
  171. }
  172. /* set/clear the stall and toggle bits for non-ep0 */
  173. void mtu3_ep_stall_set(struct mtu3_ep *mep, bool set)
  174. {
  175. struct mtu3 *mtu = mep->mtu;
  176. void __iomem *mbase = mtu->mac_base;
  177. u8 epnum = mep->epnum;
  178. u32 csr;
  179. if (mep->is_in) { /* TX */
  180. csr = mtu3_readl(mbase, MU3D_EP_TXCR0(epnum)) & TX_W1C_BITS;
  181. if (set)
  182. csr |= TX_SENDSTALL;
  183. else
  184. csr = (csr & (~TX_SENDSTALL)) | TX_SENTSTALL;
  185. mtu3_writel(mbase, MU3D_EP_TXCR0(epnum), csr);
  186. } else { /* RX */
  187. csr = mtu3_readl(mbase, MU3D_EP_RXCR0(epnum)) & RX_W1C_BITS;
  188. if (set)
  189. csr |= RX_SENDSTALL;
  190. else
  191. csr = (csr & (~RX_SENDSTALL)) | RX_SENTSTALL;
  192. mtu3_writel(mbase, MU3D_EP_RXCR0(epnum), csr);
  193. }
  194. if (!set) {
  195. mtu3_ep_reset(mep);
  196. mep->flags &= ~MTU3_EP_STALL;
  197. } else {
  198. mep->flags |= MTU3_EP_STALL;
  199. }
  200. dev_dbg(mtu->dev, "%s: %s\n", mep->name,
  201. set ? "SEND STALL" : "CLEAR STALL, with EP RESET");
  202. }
  203. void mtu3_dev_on_off(struct mtu3 *mtu, int is_on)
  204. {
  205. if (mtu->is_u3_ip && mtu->max_speed >= USB_SPEED_SUPER)
  206. mtu3_ss_func_set(mtu, is_on);
  207. else
  208. mtu3_hs_softconn_set(mtu, is_on);
  209. dev_info(mtu->dev, "gadget (%s) pullup D%s\n",
  210. usb_speed_string(mtu->max_speed), is_on ? "+" : "-");
  211. }
  212. void mtu3_start(struct mtu3 *mtu)
  213. {
  214. void __iomem *mbase = mtu->mac_base;
  215. dev_dbg(mtu->dev, "%s devctl 0x%x\n", __func__,
  216. mtu3_readl(mbase, U3D_DEVICE_CONTROL));
  217. mtu3_clrbits(mtu->ippc_base, U3D_SSUSB_IP_PW_CTRL2, SSUSB_IP_DEV_PDN);
  218. /*
  219. * When disable U2 port, USB2_CSR's register will be reset to
  220. * default value after re-enable it again(HS is enabled by default).
  221. * So if force mac to work as FS, disable HS function.
  222. */
  223. if (mtu->max_speed == USB_SPEED_FULL)
  224. mtu3_clrbits(mbase, U3D_POWER_MANAGEMENT, HS_ENABLE);
  225. /* Initialize the default interrupts */
  226. mtu3_intr_enable(mtu);
  227. mtu->is_active = 1;
  228. if (mtu->softconnect)
  229. mtu3_dev_on_off(mtu, 1);
  230. }
  231. void mtu3_stop(struct mtu3 *mtu)
  232. {
  233. dev_dbg(mtu->dev, "%s\n", __func__);
  234. mtu3_intr_disable(mtu);
  235. mtu3_intr_status_clear(mtu);
  236. if (mtu->softconnect)
  237. mtu3_dev_on_off(mtu, 0);
  238. mtu->is_active = 0;
  239. mtu3_setbits(mtu->ippc_base, U3D_SSUSB_IP_PW_CTRL2, SSUSB_IP_DEV_PDN);
  240. }
  241. /* for non-ep0 */
  242. int mtu3_config_ep(struct mtu3 *mtu, struct mtu3_ep *mep,
  243. int interval, int burst, int mult)
  244. {
  245. void __iomem *mbase = mtu->mac_base;
  246. int epnum = mep->epnum;
  247. u32 csr0, csr1, csr2;
  248. int fifo_sgsz, fifo_addr;
  249. int num_pkts;
  250. fifo_addr = ep_fifo_alloc(mep, mep->maxp);
  251. if (fifo_addr < 0) {
  252. dev_err(mtu->dev, "alloc ep fifo failed(%d)\n", mep->maxp);
  253. return -ENOMEM;
  254. }
  255. fifo_sgsz = ilog2(mep->fifo_seg_size);
  256. dev_dbg(mtu->dev, "%s fifosz: %x(%x/%x)\n", __func__, fifo_sgsz,
  257. mep->fifo_seg_size, mep->fifo_size);
  258. if (mep->is_in) {
  259. csr0 = TX_TXMAXPKTSZ(mep->maxp);
  260. csr0 |= TX_DMAREQEN;
  261. num_pkts = (burst + 1) * (mult + 1) - 1;
  262. csr1 = TX_SS_BURST(burst) | TX_SLOT(mep->slot);
  263. csr1 |= TX_MAX_PKT(num_pkts) | TX_MULT(mult);
  264. csr2 = TX_FIFOADDR(fifo_addr >> 4);
  265. csr2 |= TX_FIFOSEGSIZE(fifo_sgsz);
  266. switch (mep->type) {
  267. case USB_ENDPOINT_XFER_BULK:
  268. csr1 |= TX_TYPE(TYPE_BULK);
  269. break;
  270. case USB_ENDPOINT_XFER_ISOC:
  271. csr1 |= TX_TYPE(TYPE_ISO);
  272. csr2 |= TX_BINTERVAL(interval);
  273. break;
  274. case USB_ENDPOINT_XFER_INT:
  275. csr1 |= TX_TYPE(TYPE_INT);
  276. csr2 |= TX_BINTERVAL(interval);
  277. break;
  278. }
  279. /* Enable QMU Done interrupt */
  280. mtu3_setbits(mbase, U3D_QIESR0, QMU_TX_DONE_INT(epnum));
  281. mtu3_writel(mbase, MU3D_EP_TXCR0(epnum), csr0);
  282. mtu3_writel(mbase, MU3D_EP_TXCR1(epnum), csr1);
  283. mtu3_writel(mbase, MU3D_EP_TXCR2(epnum), csr2);
  284. dev_dbg(mtu->dev, "U3D_TX%d CSR0:%#x, CSR1:%#x, CSR2:%#x\n",
  285. epnum, mtu3_readl(mbase, MU3D_EP_TXCR0(epnum)),
  286. mtu3_readl(mbase, MU3D_EP_TXCR1(epnum)),
  287. mtu3_readl(mbase, MU3D_EP_TXCR2(epnum)));
  288. } else {
  289. csr0 = RX_RXMAXPKTSZ(mep->maxp);
  290. csr0 |= RX_DMAREQEN;
  291. num_pkts = (burst + 1) * (mult + 1) - 1;
  292. csr1 = RX_SS_BURST(burst) | RX_SLOT(mep->slot);
  293. csr1 |= RX_MAX_PKT(num_pkts) | RX_MULT(mult);
  294. csr2 = RX_FIFOADDR(fifo_addr >> 4);
  295. csr2 |= RX_FIFOSEGSIZE(fifo_sgsz);
  296. switch (mep->type) {
  297. case USB_ENDPOINT_XFER_BULK:
  298. csr1 |= RX_TYPE(TYPE_BULK);
  299. break;
  300. case USB_ENDPOINT_XFER_ISOC:
  301. csr1 |= RX_TYPE(TYPE_ISO);
  302. csr2 |= RX_BINTERVAL(interval);
  303. break;
  304. case USB_ENDPOINT_XFER_INT:
  305. csr1 |= RX_TYPE(TYPE_INT);
  306. csr2 |= RX_BINTERVAL(interval);
  307. break;
  308. }
  309. /*Enable QMU Done interrupt */
  310. mtu3_setbits(mbase, U3D_QIESR0, QMU_RX_DONE_INT(epnum));
  311. mtu3_writel(mbase, MU3D_EP_RXCR0(epnum), csr0);
  312. mtu3_writel(mbase, MU3D_EP_RXCR1(epnum), csr1);
  313. mtu3_writel(mbase, MU3D_EP_RXCR2(epnum), csr2);
  314. dev_dbg(mtu->dev, "U3D_RX%d CSR0:%#x, CSR1:%#x, CSR2:%#x\n",
  315. epnum, mtu3_readl(mbase, MU3D_EP_RXCR0(epnum)),
  316. mtu3_readl(mbase, MU3D_EP_RXCR1(epnum)),
  317. mtu3_readl(mbase, MU3D_EP_RXCR2(epnum)));
  318. }
  319. dev_dbg(mtu->dev, "csr0:%#x, csr1:%#x, csr2:%#x\n", csr0, csr1, csr2);
  320. dev_dbg(mtu->dev, "%s: %s, fifo-addr:%#x, fifo-size:%#x(%#x/%#x)\n",
  321. __func__, mep->name, mep->fifo_addr, mep->fifo_size,
  322. fifo_sgsz, mep->fifo_seg_size);
  323. return 0;
  324. }
  325. /* for non-ep0 */
  326. void mtu3_deconfig_ep(struct mtu3 *mtu, struct mtu3_ep *mep)
  327. {
  328. void __iomem *mbase = mtu->mac_base;
  329. int epnum = mep->epnum;
  330. if (mep->is_in) {
  331. mtu3_writel(mbase, MU3D_EP_TXCR0(epnum), 0);
  332. mtu3_writel(mbase, MU3D_EP_TXCR1(epnum), 0);
  333. mtu3_writel(mbase, MU3D_EP_TXCR2(epnum), 0);
  334. mtu3_setbits(mbase, U3D_QIECR0, QMU_TX_DONE_INT(epnum));
  335. } else {
  336. mtu3_writel(mbase, MU3D_EP_RXCR0(epnum), 0);
  337. mtu3_writel(mbase, MU3D_EP_RXCR1(epnum), 0);
  338. mtu3_writel(mbase, MU3D_EP_RXCR2(epnum), 0);
  339. mtu3_setbits(mbase, U3D_QIECR0, QMU_RX_DONE_INT(epnum));
  340. }
  341. mtu3_ep_reset(mep);
  342. ep_fifo_free(mep);
  343. dev_dbg(mtu->dev, "%s: %s\n", __func__, mep->name);
  344. }
  345. /*
  346. * Two scenarios:
  347. * 1. when device IP supports SS, the fifo of EP0, TX EPs, RX EPs
  348. * are separated;
  349. * 2. when supports only HS, the fifo is shared for all EPs, and
  350. * the capability registers of @EPNTXFFSZ or @EPNRXFFSZ indicate
  351. * the total fifo size of non-ep0, and ep0's is fixed to 64B,
  352. * so the total fifo size is 64B + @EPNTXFFSZ;
  353. * Due to the first 64B should be reserved for EP0, non-ep0's fifo
  354. * starts from offset 64 and are divided into two equal parts for
  355. * TX or RX EPs for simplification.
  356. */
  357. static void get_ep_fifo_config(struct mtu3 *mtu)
  358. {
  359. struct mtu3_fifo_info *tx_fifo;
  360. struct mtu3_fifo_info *rx_fifo;
  361. u32 fifosize;
  362. if (mtu->is_u3_ip) {
  363. fifosize = mtu3_readl(mtu->mac_base, U3D_CAP_EPNTXFFSZ);
  364. tx_fifo = &mtu->tx_fifo;
  365. tx_fifo->base = 0;
  366. tx_fifo->limit = fifosize / MTU3_EP_FIFO_UNIT;
  367. bitmap_zero(tx_fifo->bitmap, MTU3_FIFO_BIT_SIZE);
  368. fifosize = mtu3_readl(mtu->mac_base, U3D_CAP_EPNRXFFSZ);
  369. rx_fifo = &mtu->rx_fifo;
  370. rx_fifo->base = 0;
  371. rx_fifo->limit = fifosize / MTU3_EP_FIFO_UNIT;
  372. bitmap_zero(rx_fifo->bitmap, MTU3_FIFO_BIT_SIZE);
  373. mtu->slot = MTU3_U3_IP_SLOT_DEFAULT;
  374. } else {
  375. fifosize = mtu3_readl(mtu->mac_base, U3D_CAP_EPNTXFFSZ);
  376. tx_fifo = &mtu->tx_fifo;
  377. tx_fifo->base = MTU3_U2_IP_EP0_FIFO_SIZE;
  378. tx_fifo->limit = (fifosize / MTU3_EP_FIFO_UNIT) >> 1;
  379. bitmap_zero(tx_fifo->bitmap, MTU3_FIFO_BIT_SIZE);
  380. rx_fifo = &mtu->rx_fifo;
  381. rx_fifo->base =
  382. tx_fifo->base + tx_fifo->limit * MTU3_EP_FIFO_UNIT;
  383. rx_fifo->limit = tx_fifo->limit;
  384. bitmap_zero(rx_fifo->bitmap, MTU3_FIFO_BIT_SIZE);
  385. mtu->slot = MTU3_U2_IP_SLOT_DEFAULT;
  386. }
  387. dev_dbg(mtu->dev, "%s, TX: base-%d, limit-%d; RX: base-%d, limit-%d\n",
  388. __func__, tx_fifo->base, tx_fifo->limit,
  389. rx_fifo->base, rx_fifo->limit);
  390. }
  391. void mtu3_ep0_setup(struct mtu3 *mtu)
  392. {
  393. u32 maxpacket = mtu->g.ep0->maxpacket;
  394. u32 csr;
  395. dev_dbg(mtu->dev, "%s maxpacket: %d\n", __func__, maxpacket);
  396. csr = mtu3_readl(mtu->mac_base, U3D_EP0CSR);
  397. csr &= ~EP0_MAXPKTSZ_MSK;
  398. csr |= EP0_MAXPKTSZ(maxpacket);
  399. csr &= EP0_W1C_BITS;
  400. mtu3_writel(mtu->mac_base, U3D_EP0CSR, csr);
  401. /* Enable EP0 interrupt */
  402. mtu3_writel(mtu->mac_base, U3D_EPIESR, EP0ISR);
  403. }
  404. static int mtu3_mem_alloc(struct mtu3 *mtu)
  405. {
  406. void __iomem *mbase = mtu->mac_base;
  407. struct mtu3_ep *ep_array;
  408. int in_ep_num, out_ep_num;
  409. u32 cap_epinfo;
  410. int ret;
  411. int i;
  412. cap_epinfo = mtu3_readl(mbase, U3D_CAP_EPINFO);
  413. in_ep_num = CAP_TX_EP_NUM(cap_epinfo);
  414. out_ep_num = CAP_RX_EP_NUM(cap_epinfo);
  415. dev_info(mtu->dev, "fifosz/epnum: Tx=%#x/%d, Rx=%#x/%d\n",
  416. mtu3_readl(mbase, U3D_CAP_EPNTXFFSZ), in_ep_num,
  417. mtu3_readl(mbase, U3D_CAP_EPNRXFFSZ), out_ep_num);
  418. /* one for ep0, another is reserved */
  419. mtu->num_eps = min(in_ep_num, out_ep_num) + 1;
  420. ep_array = kcalloc(mtu->num_eps * 2, sizeof(*ep_array), GFP_KERNEL);
  421. if (ep_array == NULL)
  422. return -ENOMEM;
  423. mtu->ep_array = ep_array;
  424. mtu->in_eps = ep_array;
  425. mtu->out_eps = &ep_array[mtu->num_eps];
  426. /* ep0 uses in_eps[0], out_eps[0] is reserved */
  427. mtu->ep0 = mtu->in_eps;
  428. mtu->ep0->mtu = mtu;
  429. mtu->ep0->epnum = 0;
  430. for (i = 1; i < mtu->num_eps; i++) {
  431. struct mtu3_ep *mep = mtu->in_eps + i;
  432. mep->fifo = &mtu->tx_fifo;
  433. mep = mtu->out_eps + i;
  434. mep->fifo = &mtu->rx_fifo;
  435. }
  436. get_ep_fifo_config(mtu);
  437. ret = mtu3_qmu_init(mtu);
  438. if (ret)
  439. kfree(mtu->ep_array);
  440. return ret;
  441. }
  442. static void mtu3_mem_free(struct mtu3 *mtu)
  443. {
  444. mtu3_qmu_exit(mtu);
  445. kfree(mtu->ep_array);
  446. }
  447. static void mtu3_set_speed(struct mtu3 *mtu)
  448. {
  449. void __iomem *mbase = mtu->mac_base;
  450. if (!mtu->is_u3_ip && (mtu->max_speed > USB_SPEED_HIGH))
  451. mtu->max_speed = USB_SPEED_HIGH;
  452. if (mtu->max_speed == USB_SPEED_FULL) {
  453. /* disable U3 SS function */
  454. mtu3_clrbits(mbase, U3D_USB3_CONFIG, USB3_EN);
  455. /* disable HS function */
  456. mtu3_clrbits(mbase, U3D_POWER_MANAGEMENT, HS_ENABLE);
  457. } else if (mtu->max_speed == USB_SPEED_HIGH) {
  458. mtu3_clrbits(mbase, U3D_USB3_CONFIG, USB3_EN);
  459. /* HS/FS detected by HW */
  460. mtu3_setbits(mbase, U3D_POWER_MANAGEMENT, HS_ENABLE);
  461. } else if (mtu->max_speed == USB_SPEED_SUPER) {
  462. mtu3_clrbits(mtu->ippc_base, SSUSB_U3_CTRL(0),
  463. SSUSB_U3_PORT_SSP_SPEED);
  464. }
  465. dev_info(mtu->dev, "max_speed: %s\n",
  466. usb_speed_string(mtu->max_speed));
  467. }
  468. static void mtu3_regs_init(struct mtu3 *mtu)
  469. {
  470. void __iomem *mbase = mtu->mac_base;
  471. /* be sure interrupts are disabled before registration of ISR */
  472. mtu3_intr_disable(mtu);
  473. mtu3_intr_status_clear(mtu);
  474. if (mtu->is_u3_ip) {
  475. /* disable LGO_U1/U2 by default */
  476. mtu3_clrbits(mbase, U3D_LINK_POWER_CONTROL,
  477. SW_U1_REQUEST_ENABLE | SW_U2_REQUEST_ENABLE);
  478. /* enable accept LGO_U1/U2 link command from host */
  479. mtu3_setbits(mbase, U3D_LINK_POWER_CONTROL,
  480. SW_U1_ACCEPT_ENABLE | SW_U2_ACCEPT_ENABLE);
  481. /* device responses to u3_exit from host automatically */
  482. mtu3_clrbits(mbase, U3D_LTSSM_CTRL, SOFT_U3_EXIT_EN);
  483. /* automatically build U2 link when U3 detect fail */
  484. mtu3_setbits(mbase, U3D_USB2_TEST_MODE, U2U3_AUTO_SWITCH);
  485. }
  486. mtu3_set_speed(mtu);
  487. /* delay about 0.1us from detecting reset to send chirp-K */
  488. mtu3_clrbits(mbase, U3D_LINK_RESET_INFO, WTCHRP_MSK);
  489. /* U2/U3 detected by HW */
  490. mtu3_writel(mbase, U3D_DEVICE_CONF, 0);
  491. /* enable QMU 16B checksum */
  492. mtu3_setbits(mbase, U3D_QCR0, QMU_CS16B_EN);
  493. /* vbus detected by HW */
  494. mtu3_clrbits(mbase, U3D_MISC_CTRL, VBUS_FRC_EN | VBUS_ON);
  495. }
  496. static irqreturn_t mtu3_link_isr(struct mtu3 *mtu)
  497. {
  498. void __iomem *mbase = mtu->mac_base;
  499. enum usb_device_speed udev_speed;
  500. u32 maxpkt = 64;
  501. u32 link;
  502. u32 speed;
  503. link = mtu3_readl(mbase, U3D_DEV_LINK_INTR);
  504. link &= mtu3_readl(mbase, U3D_DEV_LINK_INTR_ENABLE);
  505. mtu3_writel(mbase, U3D_DEV_LINK_INTR, link); /* W1C */
  506. dev_dbg(mtu->dev, "=== LINK[%x] ===\n", link);
  507. if (!(link & SSUSB_DEV_SPEED_CHG_INTR))
  508. return IRQ_NONE;
  509. speed = SSUSB_DEV_SPEED(mtu3_readl(mbase, U3D_DEVICE_CONF));
  510. switch (speed) {
  511. case MTU3_SPEED_FULL:
  512. udev_speed = USB_SPEED_FULL;
  513. /*BESLCK = 4 < BESLCK_U3 = 10 < BESLDCK = 15 */
  514. mtu3_writel(mbase, U3D_USB20_LPM_PARAMETER, LPM_BESLDCK(0xf)
  515. | LPM_BESLCK(4) | LPM_BESLCK_U3(0xa));
  516. mtu3_setbits(mbase, U3D_POWER_MANAGEMENT,
  517. LPM_BESL_STALL | LPM_BESLD_STALL);
  518. break;
  519. case MTU3_SPEED_HIGH:
  520. udev_speed = USB_SPEED_HIGH;
  521. /*BESLCK = 4 < BESLCK_U3 = 10 < BESLDCK = 15 */
  522. mtu3_writel(mbase, U3D_USB20_LPM_PARAMETER, LPM_BESLDCK(0xf)
  523. | LPM_BESLCK(4) | LPM_BESLCK_U3(0xa));
  524. mtu3_setbits(mbase, U3D_POWER_MANAGEMENT,
  525. LPM_BESL_STALL | LPM_BESLD_STALL);
  526. break;
  527. case MTU3_SPEED_SUPER:
  528. udev_speed = USB_SPEED_SUPER;
  529. maxpkt = 512;
  530. break;
  531. case MTU3_SPEED_SUPER_PLUS:
  532. udev_speed = USB_SPEED_SUPER_PLUS;
  533. maxpkt = 512;
  534. break;
  535. default:
  536. udev_speed = USB_SPEED_UNKNOWN;
  537. break;
  538. }
  539. dev_dbg(mtu->dev, "%s: %s\n", __func__, usb_speed_string(udev_speed));
  540. mtu->g.speed = udev_speed;
  541. mtu->g.ep0->maxpacket = maxpkt;
  542. mtu->ep0_state = MU3D_EP0_STATE_SETUP;
  543. if (udev_speed == USB_SPEED_UNKNOWN)
  544. mtu3_gadget_disconnect(mtu);
  545. else
  546. mtu3_ep0_setup(mtu);
  547. return IRQ_HANDLED;
  548. }
  549. static irqreturn_t mtu3_u3_ltssm_isr(struct mtu3 *mtu)
  550. {
  551. void __iomem *mbase = mtu->mac_base;
  552. u32 ltssm;
  553. ltssm = mtu3_readl(mbase, U3D_LTSSM_INTR);
  554. ltssm &= mtu3_readl(mbase, U3D_LTSSM_INTR_ENABLE);
  555. mtu3_writel(mbase, U3D_LTSSM_INTR, ltssm); /* W1C */
  556. dev_dbg(mtu->dev, "=== LTSSM[%x] ===\n", ltssm);
  557. if (ltssm & (HOT_RST_INTR | WARM_RST_INTR))
  558. mtu3_gadget_reset(mtu);
  559. if (ltssm & VBUS_FALL_INTR) {
  560. mtu3_ss_func_set(mtu, false);
  561. mtu3_gadget_reset(mtu);
  562. }
  563. if (ltssm & VBUS_RISE_INTR)
  564. mtu3_ss_func_set(mtu, true);
  565. if (ltssm & EXIT_U3_INTR)
  566. mtu3_gadget_resume(mtu);
  567. if (ltssm & ENTER_U3_INTR)
  568. mtu3_gadget_suspend(mtu);
  569. return IRQ_HANDLED;
  570. }
  571. static irqreturn_t mtu3_u2_common_isr(struct mtu3 *mtu)
  572. {
  573. void __iomem *mbase = mtu->mac_base;
  574. u32 u2comm;
  575. u2comm = mtu3_readl(mbase, U3D_COMMON_USB_INTR);
  576. u2comm &= mtu3_readl(mbase, U3D_COMMON_USB_INTR_ENABLE);
  577. mtu3_writel(mbase, U3D_COMMON_USB_INTR, u2comm); /* W1C */
  578. dev_dbg(mtu->dev, "=== U2COMM[%x] ===\n", u2comm);
  579. if (u2comm & SUSPEND_INTR)
  580. mtu3_gadget_suspend(mtu);
  581. if (u2comm & RESUME_INTR)
  582. mtu3_gadget_resume(mtu);
  583. if (u2comm & RESET_INTR)
  584. mtu3_gadget_reset(mtu);
  585. if (u2comm & LPM_RESUME_INTR) {
  586. if (!(mtu3_readl(mbase, U3D_POWER_MANAGEMENT) & LPM_HRWE))
  587. mtu3_setbits(mbase, U3D_USB20_MISC_CONTROL,
  588. LPM_U3_ACK_EN);
  589. }
  590. return IRQ_HANDLED;
  591. }
  592. static irqreturn_t mtu3_irq(int irq, void *data)
  593. {
  594. struct mtu3 *mtu = (struct mtu3 *)data;
  595. unsigned long flags;
  596. u32 level1;
  597. spin_lock_irqsave(&mtu->lock, flags);
  598. /* U3D_LV1ISR is RU */
  599. level1 = mtu3_readl(mtu->mac_base, U3D_LV1ISR);
  600. level1 &= mtu3_readl(mtu->mac_base, U3D_LV1IER);
  601. if (level1 & EP_CTRL_INTR)
  602. mtu3_link_isr(mtu);
  603. if (level1 & MAC2_INTR)
  604. mtu3_u2_common_isr(mtu);
  605. if (level1 & MAC3_INTR)
  606. mtu3_u3_ltssm_isr(mtu);
  607. if (level1 & BMU_INTR)
  608. mtu3_ep0_isr(mtu);
  609. if (level1 & QMU_INTR)
  610. mtu3_qmu_isr(mtu);
  611. spin_unlock_irqrestore(&mtu->lock, flags);
  612. return IRQ_HANDLED;
  613. }
  614. static int mtu3_hw_init(struct mtu3 *mtu)
  615. {
  616. u32 cap_dev;
  617. int ret;
  618. mtu->hw_version = mtu3_readl(mtu->ippc_base, U3D_SSUSB_HW_ID);
  619. cap_dev = mtu3_readl(mtu->ippc_base, U3D_SSUSB_IP_DEV_CAP);
  620. mtu->is_u3_ip = !!SSUSB_IP_DEV_U3_PORT_NUM(cap_dev);
  621. dev_info(mtu->dev, "IP version 0x%x(%s IP)\n", mtu->hw_version,
  622. mtu->is_u3_ip ? "U3" : "U2");
  623. mtu3_device_reset(mtu);
  624. ret = mtu3_device_enable(mtu);
  625. if (ret) {
  626. dev_err(mtu->dev, "device enable failed %d\n", ret);
  627. return ret;
  628. }
  629. ret = mtu3_mem_alloc(mtu);
  630. if (ret)
  631. return -ENOMEM;
  632. mtu3_regs_init(mtu);
  633. return 0;
  634. }
  635. static void mtu3_hw_exit(struct mtu3 *mtu)
  636. {
  637. mtu3_device_disable(mtu);
  638. mtu3_mem_free(mtu);
  639. }
  640. /**
  641. * we set 32-bit DMA mask by default, here check whether the controller
  642. * supports 36-bit DMA or not, if it does, set 36-bit DMA mask.
  643. */
  644. static int mtu3_set_dma_mask(struct mtu3 *mtu)
  645. {
  646. struct device *dev = mtu->dev;
  647. bool is_36bit = false;
  648. int ret = 0;
  649. u32 value;
  650. value = mtu3_readl(mtu->mac_base, U3D_MISC_CTRL);
  651. if (value & DMA_ADDR_36BIT) {
  652. is_36bit = true;
  653. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(36));
  654. /* If set 36-bit DMA mask fails, fall back to 32-bit DMA mask */
  655. if (ret) {
  656. is_36bit = false;
  657. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  658. }
  659. }
  660. dev_info(dev, "dma mask: %s bits\n", is_36bit ? "36" : "32");
  661. return ret;
  662. }
  663. int ssusb_gadget_init(struct ssusb_mtk *ssusb)
  664. {
  665. struct device *dev = ssusb->dev;
  666. struct platform_device *pdev = to_platform_device(dev);
  667. struct mtu3 *mtu = NULL;
  668. struct resource *res;
  669. int ret = -ENOMEM;
  670. mtu = devm_kzalloc(dev, sizeof(struct mtu3), GFP_KERNEL);
  671. if (mtu == NULL)
  672. return -ENOMEM;
  673. mtu->irq = platform_get_irq(pdev, 0);
  674. if (mtu->irq < 0) {
  675. dev_err(dev, "fail to get irq number\n");
  676. return mtu->irq;
  677. }
  678. dev_info(dev, "irq %d\n", mtu->irq);
  679. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mac");
  680. mtu->mac_base = devm_ioremap_resource(dev, res);
  681. if (IS_ERR(mtu->mac_base)) {
  682. dev_err(dev, "error mapping memory for dev mac\n");
  683. return PTR_ERR(mtu->mac_base);
  684. }
  685. spin_lock_init(&mtu->lock);
  686. mtu->dev = dev;
  687. mtu->ippc_base = ssusb->ippc_base;
  688. ssusb->mac_base = mtu->mac_base;
  689. ssusb->u3d = mtu;
  690. mtu->ssusb = ssusb;
  691. mtu->max_speed = usb_get_maximum_speed(dev);
  692. /* check the max_speed parameter */
  693. switch (mtu->max_speed) {
  694. case USB_SPEED_FULL:
  695. case USB_SPEED_HIGH:
  696. case USB_SPEED_SUPER:
  697. case USB_SPEED_SUPER_PLUS:
  698. break;
  699. default:
  700. dev_err(dev, "invalid max_speed: %s\n",
  701. usb_speed_string(mtu->max_speed));
  702. /* fall through */
  703. case USB_SPEED_UNKNOWN:
  704. /* default as SSP */
  705. mtu->max_speed = USB_SPEED_SUPER_PLUS;
  706. break;
  707. }
  708. dev_dbg(dev, "mac_base=0x%p, ippc_base=0x%p\n",
  709. mtu->mac_base, mtu->ippc_base);
  710. ret = mtu3_hw_init(mtu);
  711. if (ret) {
  712. dev_err(dev, "mtu3 hw init failed:%d\n", ret);
  713. return ret;
  714. }
  715. ret = mtu3_set_dma_mask(mtu);
  716. if (ret) {
  717. dev_err(dev, "mtu3 set dma_mask failed:%d\n", ret);
  718. goto dma_mask_err;
  719. }
  720. ret = devm_request_irq(dev, mtu->irq, mtu3_irq, 0, dev_name(dev), mtu);
  721. if (ret) {
  722. dev_err(dev, "request irq %d failed!\n", mtu->irq);
  723. goto irq_err;
  724. }
  725. device_init_wakeup(dev, true);
  726. ret = mtu3_gadget_setup(mtu);
  727. if (ret) {
  728. dev_err(dev, "mtu3 gadget init failed:%d\n", ret);
  729. goto gadget_err;
  730. }
  731. /* init as host mode, power down device IP for power saving */
  732. if (mtu->ssusb->dr_mode == USB_DR_MODE_OTG)
  733. mtu3_stop(mtu);
  734. dev_dbg(dev, " %s() done...\n", __func__);
  735. return 0;
  736. gadget_err:
  737. device_init_wakeup(dev, false);
  738. dma_mask_err:
  739. irq_err:
  740. mtu3_hw_exit(mtu);
  741. ssusb->u3d = NULL;
  742. dev_err(dev, " %s() fail...\n", __func__);
  743. return ret;
  744. }
  745. void ssusb_gadget_exit(struct ssusb_mtk *ssusb)
  746. {
  747. struct mtu3 *mtu = ssusb->u3d;
  748. mtu3_gadget_cleanup(mtu);
  749. device_init_wakeup(ssusb->dev, false);
  750. mtu3_hw_exit(mtu);
  751. }